I am not able to print the output on the screen.I am using cppreference side (GCC 12.1 (c++20 )) compiler, Is there any deadlock situation in below example. Is there any online compiler i can use for trying this type of examples
#include <iostream>
#include <semaphore>
#include <thread>
#include <vector>
std::vector<int> myVec{};
std::counting_semaphore<1> prepareSignal(2); // (1)
void prepareWork()
{
myVec.insert(myVec.end(), {0, 1, 0, 3});
std::cout << "Sender: Data prepared." << '\n';
prepareSignal.release(); // (2)
}
void completeWork()
{
std::cout << "Waiter: Waiting for data." << '\n';
prepareSignal.acquire(); // (3)
myVec[2] = 2;
std::cout << "Waiter: Complete the work." << '\n';
for (auto i: myVec) std::cout << i << " ";
std::cout << '\n';
}
int main()
{
std::cout << '\n';
std::thread t1(prepareWork);
std::thread t3(completeWork);
std::thread t2(completeWork);
t1.join();
t3.join();
t2.join();
std::cout << '\n';
}