/* This is a simple example of multi-threaded programming. First, create a Win32 console app, and then confirm the following settings: project-> property-> Configuration properties-> C/C ++-> code generation-> Runtime Library-> multi-thread DLL (/MD) */# include <stdafx. h> # include <stdio. h> # include <stdlib. h> # include <windows. h> typedef struct thread_param {int threadno; char STR [20];} t_param; void * myfunc (t_param * ww); int main (INT argc, char * argv []) {int I = 0; handle h_thread [20]; t_param * mypar As; t_param * mypara; char STR [] = "test"; myparas = (t_param *) malloc (sizeof (t_param) * 20); If (myparas = NULL) {printf ("malloc error! \ N "); Return-1;} memset (myparas, 0, sizeof (t_param) * 20); for (I = 0; I <20; I ++) {mypara = myparas + I; mypara-> threadno = I; strcpy (mypara-> STR, STR); h_thread [I] = createthread (null, 0, (lpthread_start_routine) myfunc, (lpvoid) mypara, 0, null); If (h_thread [I] = NULL) {printf ("Thread No [% d] Start failed! \ N ", I); Return-2 ;}}for (I = 0; I <20; I ++) {waitforsingleobject (h_thread [I], infinite ); closehandle (h_thread [I]);} Free (myparas); printf ("excl suc !!! \ N "); Return 0;} void * myfunc (t_param * ww) {printf (" % d \ n ", WW-> threadno); Return 0 ;}
Note: Reference Article : Windows multi-threaded instance. Thank you, author -- nowdoit