Search code examples
c++linuxatomicboost-threadrace-condition

how to use boost atomic to remove race condition?


I am trying to use boost::atomic to do multithreading synchronization on linux.

But, the result is not consistent.

Any help will be appreciated.

thanks

#include <boost/bind.hpp>
#include <boost/threadpool.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
#include <boost/atomic.hpp>

boost::atomic<int> g(0) ;

void f()
{

    g.fetch_add(1, boost::memory_order_relaxed);

    return ;
 }
 const int threadnum = 10;
 int main()
 {
    boost::threadpool::fifo_pool tp(threadnum);
    for (int i = 0 ; i < threadnum ; ++i)
            tp.schedule(boost::bind(f));
    tp.wait();
    std::cout << g << std::endl ;
    return 0 ;
  }

Solution

  • I'm not familiar with the boost thread library specifically, or boost::threadpool, but it looks to me like the threads have not necessarily completed when you access the value of g, so you will get some value between zero and 10.

    Here's your program, modified to use the standard library, with joins inserted so that the fetch adds happen before the output of g.

    std::atomic<int> g(0);
    
    void f() {
        g.fetch_add(1, std::memory_order_relaxed);
    }
    
    int main() {
        const int threadnum = 10;
        std::vector<std::thread> v;
    
        for (int i = 0 ; i < threadnum ; ++i)
            v.push_back(std::thread(f));
    
        for (auto &th : v)
            th.join();
    
        std::cout << g << '\n';
    }
    

    edit:

    If your program still isn't consistent even with the added tp.wait() then that is puzzling. The adds should happen before the threads end, and I would think that the threads ending would synchronize with the tp.wait(), which happens before the read. So all the adds should happen before g is printed, even though you use memory_order_relaxed, so the printed value should be 10.