$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
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.