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 <iostream>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <conio.h>
int sortie=0;
CRITICAL_SECTION Sync;
std::vector <int> monVecteur(0);
std::vector <int> monVecteurCopy(0);
DWORD WINAPI thread1(LPVOID lpParameter)
{
while (sortie==0)
{
EnterCriticalSection(&Sync);
monVecteur.push_back(10);
LeaveCriticalSection(&Sync);
}
return 0;
}
DWORD WINAPI thread2(LPVOID lpParameter)
{
while (sortie==0)
{
EnterCriticalSection(&Sync);
monVecteurCopy.clear();
for(int i=0;i<monVecteur.size();i++)
{
monVecteurCopy.push_back(monVecteur.at(i));
}
LeaveCriticalSection(&Sync);
}
return 0;
}
int main()
{
InitializeCriticalSection(&Sync);
HANDLE hThrd1;
HANDLE hThrd2;
DWORD threadID;
hThrd1 = (HANDLE) _beginthreadex(
0,0,
(unsigned (_stdcall *)(void *)) thread1,
(LPVOID) 0,0, (unsigned *) &threadID
);
hThrd2 = (HANDLE) _beginthreadex(
0,0,
(unsigned (_stdcall *)(void *)) thread2,
(LPVOID) 0,0, (unsigned *) &threadID
);
std::cin>>sortie;
Sleep(1000);
DeleteCriticalSection(&Sync);
return(0);
} |
Partager