It is necessary that 3 threads are executed in the order of their creation. This can be done by placing WaitForSingleObject with the handle parameter of the previous thread, so that each thread waits for the previous one, but then why the critical sections? How to implement it with critical sections so that they are not just for the air, but perform synchronization. http://i.imgur.com/9bGsfcf.png

Yes, this laba). With this condition:

  1. Write a program for the console process, which consists of three threads: a, b, c. The result of the program is displayed in the main thread main. To synchronize the work of threads use critical sections.

Here is my code:

 #include <windows.h> #include <iostream> #include <conio.h> using namespace std; HANDLE hThreadA; HANDLE hThreadB; HANDLE hThreadC; int a = 2; int b = 4; int c = 8; DWORD WINAPI ThreadA(LPVOID) { a = a+b; return 0; } DWORD WINAPI ThreadB(LPVOID) { WaitForSingleObject(hThreadA,INFINITE); b++; return 0; } DWORD WINAPI ThreadC(LPVOID) { WaitForSingleObject(hThreadB,INFINITE); c = a+b; return 0; } int main() { setlocale(LC_ALL, "Russian"); // создаем поток a DWORD IDThreadA; hThreadA = CreateThread(NULL, 0, ThreadA, NULL, 0, &IDThreadA); if (hThreadA == NULL) return GetLastError(); // создаем поток b DWORD IDThreadB; hThreadB = CreateThread(NULL, 0, ThreadB, NULL, 0, &IDThreadB); if (hThreadB == NULL) return GetLastError(); // создаем поток c DWORD IDThreadC; hThreadC = CreateThread(NULL, 0, ThreadC, NULL, 0, &IDThreadC); if (hThreadC == NULL) return GetLastError(); // поток main выводит результат работы WaitForSingleObject(hThreadC,INFINITE); cout << "Результат работы:\na=" << a << "\nb=" << b << "\nc=" << c << endl; CloseHandle(hThreadA); CloseHandle(hThreadB); CloseHandle(hThreadC); getch(); } 

And how to do with critical sections, I do not know. That is, I know how to use critical sections, but I do not know how to apply them in this case. Or did I think too much about the sequence and under synchronization did you mean simply adding a critical section within each thread to eliminate the possibility of simultaneously reading and assigning one variable to two threads?

  • And by the way, unobvious task. - VladD

1 answer 1

The synchronization primitive you need is called a "barrier," possibly a "cyclic barrier." Like any other synchronization primitive, it can be implemented on critical sections + condition variables .

Here is the template for your code based on the MSDN example:

 CRITICAL_SECTION CritSection; CONDITION_VARIABLE ConditionVar; void Barrier() { EnterCriticalSection(&CritSection); // Тут надо определить, какой по счету текущий вызов. if (...) // Если еще не все потоки закончили этап - ждем, освобождая критическую секцию SleepConditionVariableCS(&ConditionVar, &CritSection, INFINITE); else // Иначе - возобновляем работу всех потоков WakeAllConditionVariable(&ConditionVar); LeaveCriticalSection(&CritSection); } 
  • Eh, with conditional variables everyone can, so not interesting. - VladD