Under normal circumstances to use multi-threaded programming MFC class library to achieve, then if we do not use MFC how to multithreaded programming? This paper will discuss this issue:
Microsoft Windows API is provided in the establishment of a new thread function CreateThread, its syntax is as follows:
HThread = CreateThread (& security_attributes, dwStackSize, ThreadProc, pParam, dwFlags, & idThread);
The first parameter is at SECURITY_ATTRIBUTES pattern of the fabric of indicators. In Windows 98 ignored parameters. In Windows NT, it is set to NULL. The second parameter is the initial for the new thread stack size, the default value is 0. In any case, the necessary dynamic extension of Windows stack size.
CreateThread the third parameter is at the thread function indicators. Function name not restrictive, but it must be in the following form:
DWORD WINAPI ThreadProc (PVOID pParam);
CreateThread the fourth ThreadProc parameters passed to the parameters. This thread and the thread can be subordinate to share data.
CreateThread the fifth parameter is usually 0, but did not immediately establish thread implementation for the flag CREATE_SUSPENDED. Thread will be suspended until ResumeThread call to resume the implementation of the thread. Sixth parameter is an indicator, pointing to accept threaded ID value of the variable.
Most Windows programs writers like to use PROCESS.H table in the first statement in the document during the implementation of the C library function _ beginthread links. Its syntax is as follows:
HThread = _beginthread (ThreadProc, uiStackSize, pParam);
It is more simple, for the majority of applications are perfect, this thread function syntax:
Void __cdecl ThreadProc (void * pParam);
In the establishment of a multi-threaded Windows programs, it needs to "Project Settings" dialog box to do some modifications. Select "C / C + +" tab, and then "Category" drop-down list box, select the "Code Generation." "Use Run-Time Library" drop-down list box, we can see that for the "Release" set "Single-Threaded" and Debug settings for the "Debug Single-Threaded." These will be changed to "Multithreaded" and "Debug Multithreaded." This will be the compiler flag / MT, it is in the compiler compiler multithreaded applications needs.
The first demo.
/************************************************* ******
*
* Deom1 - four threads at the same time write a paper (no parameters)
*
*
************************************************** *********/
# Include <windows.h>
# Include <process.h> / * _beginthread, _endthread * /
# Include <iostream>
# Include <fstream>
Using namespace std;
Ofstream out ( "out.txt");
Void ThreadFunc1 (PVOID param)
(
While (1)
(
Sleep (10);
Out << "This was draw by thread l" <<endl;
)
)
Void ThreadFunc2 (PVOID param)
(
While (1)
(
Sleep (10);
Out << "This was draw by thread 2" <<endl;
)
)
Void ThreadFunc3 (PVOID param)
(
While (1)
(
Sleep (10);
Out << "This was draw by thread 3" <<endl;
)
)
Void ThreadFunc4 (PVOID param)
(
While (1)
(
Sleep (10);
Out << "This was draw by thread 4" <<endl;
)
)
Int main ()
(
Int i = 0;
_beginthread (ThreadFunc1, 0, NULL);
_beginthread (ThreadFunc2, 0, NULL);
_beginthread (ThreadFunc3, 0, NULL);
_beginthread (ThreadFunc4, 0, NULL);
Sleep (3000);
Out << "end";
Return 0;
)
/ / Demo1 end ---------------------------------------------- --
The second demo.
/************************************************* ******
*
* Deom2 - four threads at the same time write a paper (parameters)
*
*
************************************************** *********/
# Include <windows.h>
# Include <process.h> / * _beginthread, _endthread * /
# Include <iostream>
# Include <fstream>
# Include <string>
Using namespace std;
Ofstream out ( "out.txt");
Void ThreadFunc1 (PVOID param)
(
While (1)
(
Char * p;
P = (char *) param;
Sleep (10);
Out <<p << "This was draw by thread l" <<endl;
)
)
Void ThreadFunc2 (PVOID param)
(
While (1)
(
Sleep (10);
Out << "This was draw by thread 2" <<endl;
)
)
Void ThreadFunc3 (PVOID param)
(
While (1)
(
Sleep (10);
Out << "This was draw by thread 3" <<endl;
)
)
Void ThreadFunc4 (PVOID param)
(
While (1)
(
Sleep (10);
Out << "This was draw by thread 4" <<endl;
)
)
Int main ()
(
Char * pstr = "parameter transmission success";
_beginthread (ThreadFunc1, 0, pstr);
_beginthread (ThreadFunc2, 0, NULL);
_beginthread (ThreadFunc3, 0, NULL);
_beginthread (ThreadFunc4, 0, NULL);
Sleep (1000);
Out << "end";
Return 0;
)
/ / Demo2 end ---------------------------------------------- --
The third demo (a win32 application)
/************************************************* ******
*
* Deom3 - random draw on the screen a series of rectangular
*
*
************************************************** *********/
# Include <windows.h>
# Include <process.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
HWND hwnd;
Int cxClient, cyClient;
Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
(
Static TCHAR szAppName [] = TEXT ( "RndRctMT");
MSG msg;
WNDCLASS wndclass;
Wndclass.style = CS_HREDRAW | CS_VREDRAW;
Wndclass.lpfnWndProc = WndProc;
Wndclass.cbClsExtra = 0;
Wndclass.cbWndExtra = 0;
Wndclass.hInstance = hInstance;
Wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
Wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
Wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
Wndclass.lpszMenuName = NULL;
Wndclass.lpszClassName = szAppName;
If (! RegisterClass (& wndclass))
(
MessageBox (NULL, TEXT ( "This program requires Windows NT!"), SzAppName, MB_IConERROR);
Return 0;
)
Hwnd = CreateWindow (szAppName, TEXT ( "Random Rectangles")
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
While (GetMessage (& msg, NULL, 0, 0))
(
TranslateMessage (& msg);
DispatchMessage (& msg);
)
Return msg.wParam;
)
VOID Thread (PVOID pvoid)
(
HBRUSH hBrush;
HDC hdc;
Int xLeft, xRight, yTop, yBottom, iRed, iGreen, iBlue;
While (TRUE)
(
If (cxClient! = 0 | | cyClient! = 0)
(
XLeft = rand ()% cxClient;
XRight = rand ()% cxClient;
YTop = rand ()% cyClient;
YBottom = rand ()% cyClient;
IRed = rand () & 255;
IGreen = rand () & 255;
IBlue = rand () & 255;
Hdc = GetDC (hwnd);
HBrush = CreateSolidBrush (RGB (iRed, iGreen, iBlue));
SelectObject (hdc, hBrush);
Rectangle (hdc, min (xLeft, xRight), min (yTop, yBottom)
Max (xLeft, xRight), max (yTop, yBottom));
ReleaseDC (hwnd, hdc);
DeleteObject (hBrush);
)
)
)
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
(
Switch (message)
(
Case WM_CREATE:
_beginthread (Thread, 0, NULL);
Return 0;
Case WM_SIZE:
CxClient = LOWORD (lParam);
CyClient = HIWORD (lParam);
Return 0;
Case WM_DESTROY:
PostQuitMessage (0);
Return 0;
)
Return DefWindowProc (hwnd, message, wParam, lParam);
)
/ / Demo4 end ---------------------------------------------- -- |