Source.c 710 B

1234567891011121314151617181920212223242526272829303132
  1. #include <Windows.h>
  2. #include <Stdio.h>
  3. DWORD WINAPI thread_func(int thread_num);
  4. CRITICAL_SECTION cs;
  5. int count = 0;
  6. DWORD WINAPI thread_func(int thread_num) {
  7. for (int i = 0; i < 10; i++) {
  8. EnterCriticalSection(&cs);
  9. printf("Thread %d: %d\n", thread_num, count);
  10. count++;
  11. LeaveCriticalSection(&cs);
  12. Sleep(100);
  13. }
  14. return 0;
  15. }
  16. int main() {
  17. InitializeCriticalSection(&cs);
  18. HANDLE thread1 = CreateThread(NULL, 0, thread_func, 1, 0, NULL);
  19. HANDLE thread2 = CreateThread(NULL, 0, thread_func, 2, 0, NULL);
  20. WaitForSingleObject(thread1, INFINITE);
  21. WaitForSingleObject(thread2, INFINITE);
  22. DeleteCriticalSection(&cs);
  23. return 0;
  24. }