Typedef struct _ threadinof {bool * bexitflag; // exit flag (shared) handle hexitevent; // exit event (shared) handle hrunevent; // run event handle hdoneevent; // run the completed event bool bthreadisruning; // uint uthreadindex in the running thread; // The uint utaskid in the thread index; // The task id lpcritical_section lpcriticalsection; // The critical section //...... Other information} threadinof; uint threadfunction (lpvoid lparam) {threadinof * pthreadinfo = (threadinof *) lparam; // parameter check assert (pthreadinfo); Assert (pthreadinfo-> bexitflag ); assert (pthreadinfo-> hexitevent); Assert (pthreadinfo-> hrunevent); Assert (pthreadinfo-> hdoneevent );//...... Other initialization tasks while (pthreadinfo) {// wait for event handle hmultevent [] = {pthreadinfo-> hexitevent, pthreadinfo-> hrunevent}; Switch (waitformultipleobjects (2, hmultevent, false, infinite) {Case (wait_object_0): // exit event {return-1;} case (wait_object_0 + 1): // run event {break;} default: // error {assert (false); break ;}} pthreadinfo-> bthreadisruning = true; // bool btreaddone = false; (;(! Btreaddone) & (* pthreadinfo-> bexitflag = 0 );){//...... Code run by a thread // simulate some long-time operations {sleep (RAND ()/32);} // some required critical section operations if (pthreadinfo-> lpcriticalsection) entercriticalsection (pthreadinfo-> lpcriticalsection); // trace (_ T ("thead (% u) task (% u) \ n"), pthreadinfo-> uthreadindex, pthreadinfo-> utaskid); // exit the critical section if (pthreadinfo-> lpcriticalsection) leavecriticalsection (pthreadinfo-> lpcriticalsection); if (1) // meet the thread task completion condition {btreaddone = true; break; // jump out of the loop} else // set aside for other threads Time {sleep (100); // switchtothread (); // switching thread} // The thread is running completely, notifying the parent process {setevent (pthreadinfo-> hdoneevent ); pthreadinfo-> bthreadisruning = false; // thread end} return 0;} void runtask () {# define max_thread_num 4 // bool btreadexitflag = false; // The thread exit flag handle hexitevent = createevent (null, true, false, null); // The thread exit event handle hrunevent [max_thread_num] = {0 }; // notification thread running event handle hdoneevent [max_thread_num] = {0 };/ /Threadinof threadinfo [max_thread_num] = {0}; handle hthread [max_thread_num] = {0}; critical_section mcriticalsection; initializecriticalsection (& mcriticalsection ); // create thread {for (INT I = 0; I <max_thread_num; I ++) {verify (hrunevent [I] = createevent (null, false, false, null )); verify (hdoneevent [I] = createevent (null, true, true, null); threadinfo [I]. uthreadindex = I; threadinfo [I]. bexitflag = & Btreadexitflag; threadinfo [I]. hexitevent = hexitevent; threadinfo [I]. hrunevent = hrunevent [I]; threadinfo [I]. hdoneevent = hdoneevent [I]; threadinfo [I]. lpcriticalsection = & mcriticalsection ;//...... Other parameters: hthread [I] = createthread (null, 0, (lpthread_start_routine) threadfunction, & threadinfo [I], 0, null); Assert (hthread [I]);} // running thread uint utaskid = 0; bool boverflag = false; while (1) {If (boverflag) // task end {waitformultipleobjects (max_thread_num, hdoneevent, true, infinite ); // wait for all threads to end break;} else // execute the task {DWORD dwwait = waitformultipleobjects (max_thread_num, hdoneevent, false, infinite); If (dwwait >=( wait_object_0 + max_thread_num )) {assert (false);} else {int ithread = dwwait; If (utaskid <100) // a new task can be assigned {// assign a new task // threadinfo [I]. xxx sets the New Parameter threadinfo [ithread]. utaskid = utaskid ++; // resetevent (threadinfo [ithread]. hdoneevent); // reset ready event setevent (threadinfo [ithread]. hrunevent); // activation thread} else // all tasks are completed {boverflag = true ;}}// end thread {btreadexitflag = true; setevent (hexitevent ); waitformultipleobjects (max_thread_num, hthread, true, infinite) ;}// cleanup {# define doclosehandle (h) do {If (h) closehandle (h); H = NULL ;} while (0); doclosehandle (hexitevent); For (INT I = 0; I <max_thread_num; I ++) {doclosehandle (hrunevent [I]); doclosehandle (hdoneevent [I]);} deletecriticalsection (& mcriticalsection );}}
// Debug the output
Thead (2) task (2)
Thead (3) task (3)
Thead (1) task (1)
Thead (0) task (0)
Thead (3) task (5)
Thead (2) task (4)
Thead (1) task (6)
Thead (0) task (7)
Thead (1) task (9)
Thead (2) task (8)
Thead (3) task (10)
Thead (0) task (11)
Thead (1) task (12)
Thead (2) task (13)
Thead (0) task (15)
Thead (3) task (14)
Thead (1) task (16)
Thead (2) task (17)
Thead (0) task (18)
Thead (3) task (19)
Thead (1) task (20)
Thead (0) task (21)
Thead (3) task (23)
Thead (2) task (22)
Thead (1) task (24)
Thead (0) task (25)
Thead (3) task (27)
Thead (2) task (26)
Thead (1) task (28)
Thead (3) task (31)
Thead (0) task (29)
Thead (2) task (30)
Thead (1) task (32)
Thead (2) task (35)
Thead (0) task (33)
Thead (3) task (34)
Thead (1) task (36)
Thead (3) task (39)
Thead (0) task (37)
Thead (2) task (38)
Thead (1) task (40)
Thead (0) task (41)
Thead (3) task (43)
Thead (2) task (42)
Thead (1) task (44)
Thead (3) task (47)
Thead (0) task (45)
Thead (2) task (46)
Thead (1) task (48)
Thead (0) task (49)
Thead (2) task (51)
Thead (3) task (50)
Thead (1) task (52)
Thead (3) task (55)
Thead (0) task (53)
Thead (2) task (54)
Thead (1) task (56)
Thead (3) task (57)
Thead (0) task (58)
Thead (2) task (59)
Thead (1) task (60)
Thead (3) task (61)
Thead (0) task (62)
Thead (2) task (63)
Thead (1) task (64)
Thead (3) task (65)
Thead (0) task (66)
Thead (2) task (67)
Thead (1) task (68)
Thead (3) task (69)
Thead (0) task (70)
Thead (2) task (71)
Thead (1) task (72)
Thead (3) task (73)
Thead (0) task (74)
Thead (2) task (75)
Thead (1) task (76)
Thead (3) task (77)
Thead (0) task (78)
Thead (2) task (79)
Thead (1) task (80)
Thead (3) task (81)
Thead (0) task (82)
Thead (2) task (83)
Thead (1) task (84)
Thead (3) task (85)
Thead (0) task (86)
Thead (2) task (87)
Thead (1) task (88)
Thead (1) task (92)
Thead (3) task (89)
Thead (0) task (90)
Thead (2) task (91)
Thead (1) task (93)
Thead (3) task (94)
Thead (0) task (95)
Thead (2) task (96)
Thead (3) task (98)
Thead (0) task (99)
Thead (1) task (97)
The thread 'win32 thread' (0xcd4) has exited and the returned value is-1 (0 xffffffff ).
The thread 'win32 thread' (0xe58) has exited and the returned value is-1 (0 xffffffff ).
The thread 'win32 thread' (0xf94) has exited and the returned value is-1 (0 xffffffff ).
The thread 'win32 thread' (0xce4) has exited and the returned value is-1 (0 xffffffff ).