Boost logo

Boost :

From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2023-02-23 17:41:06


On 2/23/23 19:14, oliver.kowalke--- via Boost wrote:
> Code from namespace detail is not part of the public API. Do  not use this code (boost::fiber::detail::futex).

Also, if you can't use waiting/notifying operations in std::atomic, I
suggest using Boost.Atomic instead.

> 23.02.2023 14:51:39 tsoumplekas_giorgos via Boost <boost_at_[hidden]>:
>
>> 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 ?

Who is going to wake the first thread?

Also, your code is not protected against spurious wakeups.


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