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 8062809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:47:55+00:00 2026-06-05T10:47:55+00:00

I am doing a multhread C++ boost on Linux. The following program still have

  • 0

I am doing a multhread C++ boost on Linux.

The following program still have race conditions even though I tried to use locks.

The result is 8 or 9 or 5 . it should NOT happen.

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

 boost::mutex myMutex ;
 int g = 0 ;

 void f()
 {

    //myMutex.lock();
    {
            boost::mutex::scoped_lock lock(myMutex);
            ++g;
    }
    //myMutex.unlock();
    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));
    std::cout << g << std::endl ;
    return 0 ;
 }

Any help will be appreciated.

thanks !

  • 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-05T10:47:57+00:00Added an answer on June 5, 2026 at 10:47 am

    from http://threadpool.sourceforge.net/tutorial/intro.html :

    It is very important to understand that the task is only scheduled for
    execution. Schedule returns immediately and there are no guarantees
    about when the tasks are executed and how long the processing will
    take.

    You schedule 10 tasks and then immediately print the result for as many as executed by the time you reach the line

    std::cout << g << std::endl ;

    So, while your mutex makes sure threads increment g one at a time, you’re not waiting for them to finish before printing the result.
    One way to modify your code is to wait for all tasks in the pool to finish:

    boost::threadpool::fifo_pool tp(threadnum);
    for (int i = 0 ; i < threadnum ; ++i)
            tp.schedule(boost::bind(f));
    tp.wait(); //WAIT FOR TASKS TO EXECUTE
    std::cout << g << std::endl ;
    return 0 ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing a custom Tumblr theme that will have a mix of the usual Tumblr
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Doing my first SL4 MVVM RIA based application and i ran into the following
i have the following code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using
i have the following code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using
I am doing C++ multithread programming. I use mutex to read and write a
Doing some code reviews lately I came across a number of classes that have
Im doing something like this to get a list of all users that have
Doing cross platform development with 64bit. Using gcc/linux and msvc9/server 2008. Just recently deployed
Doing a german site, and have a JS alert to say you havn't completed

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.