1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include "stdafx.h"
#include <windows.h>
using namespace std;
#define nombre_threads 15
HANDLE hEventsThread[15];
HANDLE hEventsWaitForAllThreadsCreates[15];
static int numero;
DWORD WINAPI Thread1(LPVOID lpParameter)
{
numero++;
WaitForSingleObject(hEventsWaitForAllThreadsCreates[numero],INFINITE);
printf("Ceci est ma thread numero%i\n",numero);
SetEvent(hEventsThread[numero]);
return 0;
}
int _tmain()
{
numero = 0 ;
for (int i=0 ; i< nombre_threads ; i++)
{
hEventsThread[i] = CreateEvent(
NULL, // default security attributes
FALSE, // auto-reset event object
FALSE, // initial state is nonsignaled
NULL);
hEventsWaitForAllThreadsCreates[i] = CreateEvent(
NULL, // default security attributes
FALSE, // auto-reset event object
FALSE, // initial state is nonsignaled
NULL);
}
DWORD t1 = GetTickCount();
HANDLE hThread[nombre_threads];
unsigned long ThreadId;
for( i=1;i<=nombre_threads;i++)
{
hThread [i] = CreateThread(NULL,NULL,Thread1,NULL, NULL, &ThreadId);
}
DWORD t2 = GetTickCount();
printf("\n\n %i\n\n",t2-t1);
for( i=1;i<=nombre_threads;i++)
{
SetEvent(hEventsWaitForAllThreadsCreates[i]);
}
WaitForMultipleObjects( //Attente que tous les SBAs aient bien pris en compte la notif_paquet.
nombre_threads, // number of objects in array
hEventsThread, // array of objects
TRUE, // wait for all
INFINITE); // indefinite wait
DWORD t3 = GetTickCount();
printf("\n\n %i\n\n",t3-t1);
cin.get();
return 0;
} |
Partager