Write multi-thread code in YC ++
When running a program, many time-consuming operations or calculations (such as reading network files through a socket and solving a differential equation) may cause the interface to become unresponsive. This phenomenon is called blocking. How can we solve the blocking problem? The best way is to use multithreading.
In YC ++ programming, multi-threaded programs are easy to write. The most important thing to write multi-threaded programs is to use local variables whenever possible,
Do not use global variables (except read-only ). Because global variables are prone to errors.
/*************************************** **************************************** **********************************/
The following example uses the Win32 function createthread to create a thread:
Save the following code to a file named after, such as th. cpp.
In YC ++, call <file/open or create CPP source program> TH. cpp, and run <tool/execute> TH. cpp
In dos, use ycc th. cpp to generate th.exe, and then run th.exe
In VC ++, use Cl th. cpp to generate th.exe and then run th.exe
//////////////////////////////////////// //////////////////////////////////////// ////////////////////////////
# Ifndef ycc
# Include <windows. h>
# Include "include/ycapi. H"
# Pragma comment (Lib, "gdi32.lib ")
# Pragma comment (Lib, "user32.lib ")
# Pragma comment (Lib, "yxbapi. lib ")
# Endif
//////////////////////////////////////// //////////////////////////////////////// ////////////////////////////
Int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd)
{
//////// Create the main window. Use the API function of YC ++ to create it. You do not need to register it.
Int winapi mainwndproc (hwnd, uint iMessage, uint wparam, long lparam, void * puserdata );
Hwnd = yxb_window (mainwndproc, null, 0,
Ws_overlappedwindow | ws_caption | ws_sysmenu,
"Multithreading", 800,600, null, wt_win );
MSG;
While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Return 0;
}
Int winapi mainwndproc (hwnd, uint iMessage, uint wparam, long lparam, void * puserdata)
{
Static handle linep_thread_handle, rectp_thread_handle;
Switch (iMessage)
{
Case wm_destroy:
Terminatethread (linep_thread_handle, 0 );
Closehandle (linep_thread_handle );
Terminatethread (rectp_thread_handle, 0 );
Closehandle (rectp_thread_handle );
Postquitmessage (0 );
Return false;
Case wm_create:
DWORD linep_thread_ret, rectp_thread_ret;
Void draw_linep (lpvoid * my_buf), draw_rectp (lpvoid * my_buf );
Linep_thread_handle = createthread (null, 0, (lpthread_start_routine) draw_linep, hwnd, 0, & linep_thread_ret );
Rectp_thread_handle = createthread (null, 0, (lpthread_start_routine) draw_rectp, hwnd, 0, & rectp_thread_ret );
Return false;
Case wm_paint:
Paintstruct pS;
Beginpaint (hwnd, & PS );
Yimg;
Memset (& yimg, 0, sizeof yimg );
Yimg. HDC = ps. HDC; // you can use this setting to operate the window client area as an image.
Rect mrect;
Getclientrect (hwnd, & mrect );
Yxb_imagefill (& yimg, 0, 0, mrect. Right, mrect. Bottom, RGB (130,150,160 ));
Endpaint (hwnd, & PS );
Return false;
}
Return defwindowproc (hwnd, iMessage, wparam, lparam );
}
Const int mycolor [] = {0xff7766, 0xcc88ff, 0x6688aa, 0xcc9977, 0x88ff11, 0x779911, 0xdd88aa, 0x55ccff };
Void draw_linep (lpvoid * my_buf)
{
Static int xx0 = 388, yy0 = 200;
Hwnd = (hwnd) my_buf;
HDC = getdc (hwnd );
Rect mrect;
Getclientrect (hwnd, & mrect );
Movetoex (HDC, 0, 0, null );
For (int ii = 0; II ++)
{
Hpen = createpen (ps_solid, 3, mycolor [II % 8]);
Hpen hpensave = (Hpen) SelectObject (HDC, Hpen );
Lineto (HDC, xx0, yy0 );
Xx0 = rand () * mrect. Right/2/rand_max;
Yy0 = rand () * mrect. Bottom/rand_max;
SelectObject (HDC, hpensave );
Deleteobject (Hpen );
Sleep (10 );
}
Releasedc (hwnd, HDC );
}
Void draw_rectp (lpvoid * my_buf)
{
Static int xx0 = 0, yy0 = 0;
Hwnd = (hwnd) my_buf;
HDC = getdc (hwnd );
Rect mrect;
Getclientrect (hwnd, & mrect );
For (int ii = 0; II ++)
{
Hbrush HBr = (hbrush) SelectObject (HDC, createsolidbrush (mycolor [II % 8]);
Patblt (HDC, xx0, yy0, 30,30, patcopy );
Deleteobject (SelectObject (HDC, HBr ));
Xx0 = rand () * mrect. Right/2/rand_max + mrect. Right/2;
Yy0 = rand () * mrect. Bottom/rand_max;
Sleep (10 );
}
Releasedc (hwnd, HDC );
}