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

  • Home
  • SEARCH
  • 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 7505843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:03:19+00:00 2026-05-29T22:03:19+00:00

Latelly I’ve been working with multi-thread coding, after a while writing I realized that

  • 0

Latelly I’ve been working with multi-thread coding, after a while writing I realized that if I used std::cout in different boost::threads, the output would came without a logical order, the program that I’m testing is something like:

#include <boost/thread/thread.hpp>
#include <iostream>

int my01( void )
{
    std::cout << "my01" << std::endl;
    return 0;
}
/* my02, my03 and my04 are the same with different outputs*/
[...]
int main( void )
{
    boost::thread t1(&my01);
    boost::thread t2(&my02);
    boost::thread t3(&my03);
    boost::thread t4(&my04);

    while(!t1.joinable() || !t2.joinable() || !t3.joinable() || !t4.joinable());

    t1.join();
    t2.join();
    t3.join();
    t4.join();

    std::cout << "The end!" << std::endl;
    getchar();
    return 0;
}

And the output is usually like (it changes):

my02my01
my04
my03
BLANK LINE
The end!

With this issue in mind I was thinking of creating a single thread to manage all of the outputs, so they would be in order like:

my01
my02
my03
my04
The end!

Which is the optimal way to write such thread or to manage those outputs?
Please read the answers to this question too: Is cout synchronized/thread-safe?

Ps:I’m using Visual C++ 2010 Express and my cpu has 8 different cores.

Thank you for your time!

  • 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-05-29T22:03:20+00:00Added an answer on May 29, 2026 at 10:03 pm

    First of all, you might consider avoiding all the explicit thread management, and instead use std::async to launch your tasks in some arbitrary number of separate threads.

    Second, instead of doing the I/O in the threads themselves, you want to create results, and do the output itself serially. This means the thread function just creates some data, and leaves it to the caller to actually write that out:

    std::string process(int value) {
         std::ostringstream buffer;
         buffer << "my" << std::setfill('0') << std::setw(2) << value;
         return buffer.str();
    }
    

    Then we need to launch four copies of that asychronously:

    std::vector<std::future<std::string> > results;
    
    for (int i=0; i<4; i++)
        results.push_back(std::async(std::launch::async, process, i));
    

    Then we get the results and print them out in order:

    for (auto &r : results)
        std::cout << r.get() << "\n";
    

    Putting those together, we could get code like this:

    #include <string>
    #include <iostream>
    #include <thread>
    #include <future>
    #include <sstream>
    #include <vector>
    #include <iomanip>
    
    std::string process(int value) {
         std::ostringstream buffer;
         buffer << "my" << std::setfill('0') << std::setw(2) << value;
         return buffer.str();
    }
    
    int main() { 
        std::vector<std::future<std::string>> rets;
    
        for (int i=0; i<4; i++)
            rets.push_back(std::async(std::launch::async, process, i));
    
        for (auto & t : rets) {
            t.wait();
            std::cout << t.get() << "\n";
        }
    }
    

    I should add one minor point: I’m basing this on standard C++11 futures. I believe the basic idea should also work with Boost futures (upon which the standard was based) but I haven’t tested that. I’d expect that some minor adjustments (e.g., to names) will be needed to work with Boost’s futures.

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

Sidebar

Related Questions

Lately I've been writing FFI code that returns a data structure in the IO
Lately I've been working on stored procedure and encountered 1 strange problem. First, I
Lately I've been writing some JS code using jQuery and JavaScript as it is
Lately I've been wondering if there's a difference between initializing the variables that have
Lately, I've been reading some code that has if (! (a == b) )
lately I have been writing a lot of tiny plugins for my web projects
Lately I have been finding that some of the ColdFusion applications on my production
Lately I've been working on implementing security for my web application, running on a
Lately I realized that I had lost some files in one of my projects
Latelly I've seen a lot of PHP/MySQL questions that enclose SQL values within {}

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.