Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8075893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:08:53+00:00 2026-06-05T15:08:53+00:00

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

  • 0

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 ;
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:08:55+00:00Added an answer on June 5, 2026 at 3:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the timed_wait from boost. Now I am actually not
I'm trying to use the Gamma distribution from boost::math but it looks like it
I'm new to Boost, but not to functional programming, and I'm trying to see
I'm trying to use Boost's multi-index container for fast look up, but I'm having
I'm trying to use the boost library in my code but get the following
I am trying to use Boost's lexical_cast for my C++ project but am running
I'm trying to use the boost threads, but when running the program i get
I'm trying to use boost unit testing alongside the Allegro graphics library, but both
I am trying to use BOOST ASIO library asynchronous sockets. My platform is Linux.
I'm trying to use boost::thread but I can't compile because of an error compiling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.