I am studying about futex synchronisation Following up the manual , I tried to syncroniaze threads using boost futex However the threads never wake up
#include <iostream>
#include <atomic>
#include <boost/fiber/detail/futex.hpp>
#include <thread>
#include <vector>
#include <chrono>
void wait(std::atomic<int> & futexp)
{
int one = 1;
while(std::atomic_compare_exchange_strong(&futexp, &one, 0))
{}
std::cout << std::this_thread::get_id() << "," << futexp.load() << std::endl;
boost::fibers::detail::futex_wait(&futexp,0);
// std::this_thread::sleep_for(std::chrono::milliseconds(5000));
}
void fpost(std::atomic<int> & futexp)
{
int zero = 0;
if (std::atomic_compare_exchange_strong(&futexp, &zero, 1)) {
boost::fibers::detail::futex_wake(&futexp);
}
}
std::atomic<int> futexp{1};
int main() {
std::vector<std::thread> vec;
for(int i =0 ; i< 5;++i)
{
vec.emplace_back([]()
{
wait(futexp);
fpost(futexp);
});
}
for(auto & el : vec)
{
el.join();
}
}
I am using boost 1.66 and gcc 9.1 What goes wrong with this piece of code ?