#include <windows.h> #include <stdio.h>
int x = 0; bool finished = false; CRITICAL_SECTION sec;
DWORD WINAPI ThreadProc(LPVOID lpParameter) { bool end;
EnterCriticalSection(&sec); end = finished; LeaveCriticalSection(&sec);
while(!end) { EnterCriticalSection(&sec); ++x; LeaveCriticalSection(&sec); }
return 0; }
int main() { InitializeCriticalSection(&sec); DWORD dw; HANDLE thread = CreateThread(NULL, 0, ThreadProc, NULL, 0, &dw); Sleep(1000);
EnterCriticalSection(&sec); finished = true; LeaveCriticalSection(&sec);
printf("La variable x vaut %d\n", x);
DeleteCriticalSection(&sec); CloseHandle(thread);
return 0; }
Sachant qu'on peut toujours enlever une ligne à un programme, et que dans un programme il y a toujours un bug, un programme peut se résumer à une ligne avec un bug.
|