Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1.     // create threads here
  2.     hT[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)T1, 0, CREATE_SUSPENDED, &t_id1);
  3.     hT[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)T2, 0, CREATE_SUSPENDED, &t_id2);
  4.     hT[2] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)T3, 0, CREATE_SUSPENDED, &t_id3);
  5.     // create semaphores here
  6.     //= CreateSemaphore(NULL, 0, 3, NULL);
  7.     sm[0] = CreateSemaphore(NULL, 0, 3, NULL);
  8.     sm[1] = CreateSemaphore(NULL, 0, 3, NULL);
  9.     sm[2] = CreateSemaphore(NULL, 0, 3, NULL);
  10.  
  11.  
  12. void T1(void)
  13. {
  14.     cout << "T1 STARTED" << endl;
  15.     Sleep(1000);
  16.     ReleaseSemaphore(sm[0], 3, NULL);
  17.     WaitForMultipleObjects(3, sm, TRUE, INFINITE);
  18.     cout << "T1 FINISHED" << endl;
  19. }
  20.  
  21. void T2(void)
  22. {
  23.     cout << "T2 STARTED" << endl;
  24.     Sleep(2000);
  25.     ReleaseSemaphore(sm[1], 3, NULL);
  26.     WaitForMultipleObjects(3, sm, TRUE, INFINITE);
  27.     cout << "T2 FINISHED" << endl;
  28. }
  29.  
  30. void T3(void)
  31. {
  32.     cout << "T3 STARTED" << endl;
  33.     Sleep(3000);
  34.     ReleaseSemaphore(sm[2], 3, NULL);
  35.     WaitForMultipleObjects(3, sm, TRUE, INFINITE);
  36.     cout << "T3 FINISHED" << endl;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement