Windows threads (using event synchronization) are enhanced by 2008.10.9
- # Include <stdio. h>
- # Include <windows. h>
- // Thread Functions
- DWORD winapi mythreadproc (lpvoid lpparameter );
- // User-defined class
- Class mythreadparam {
- Public:
- Void myfunc ();
- };
- Handle EV; // event handle
- Int main (INT argc, char * argv []) {
- DWORD mythreadid; // thread ID
- Mythreadparam threadparam; // thread parameter, which can pass any type of pointer
- Handle h; // thread handle
- // Create an event. The initial event is "no signal" and is manually reset.
- Ev = createevent (null, true, false, "myeventname ");
- H = createthread (null, // thread attribute
- 0, // stack size, 0 use default stack size
- Mythreadproc, // thread function
- & Threadparam, // thread parameter, lpvoid type
- 0, // thread flag
- & Mythreadid // thread ID, output value
- );
- Printf ("thread ID is % d/N", mythreadid );
- // Wait for the event to signal, that is, wait for the thread to end
- Waitforsingleobject (EV, infinite );
- Printf ("thread % d terminated! /N ", mythreadid );
- // Reset the signal to no signal
- Resetevent (EV );
- Return 0;
- }
- DWORD winapi mythreadproc (lpvoid lpparameter ){
- Mythreadparam * Param = (mythreadparam *) lpparameter;
- For (INT I = 0; I <10; I ++ ){
- Param-> myfunc ();
- }
- // Set the event to a signal
- Setevent (EV );
- Return 0;
- }
- Void mythreadparam: myfunc (){
- Printf ("Hello thread! /N ");
- }