Boost logo

Boost :

From: tsoumplekas_giorgos (getsoubl_at_[hidden])
Date: 2023-02-23 09:36:30


Subject: Futex using boost, threads never wakeup
Hello all
I posted the following question in stackoverflow
https://stackoverflow.com/questions/75538799/futex-using-boost-threads-never-wakeup
0
<https://stackoverflow.com/posts/75538799/timeline>

I am studying about futex synchronisation Following up the manual
<https://man7.org/linux/man-pages/man2/futex.2.html> , 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);
        });
    }

Code demo <https://godbolt.org/z/94hMsrW4a>

I am using boost 1.66 and gcc 9.1 What goes wrong with this piece of code ?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk