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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:10:01+00:00 2026-05-14T23:10:01+00:00

I don’t see synchronized output when i comment the the line wait(1) in thread()

  • 0

I don’t see synchronized output when i comment the the line wait(1) in thread(). can I make them run at the same time (one after another) without having to use ‘wait(1)’?

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

void wait(int seconds) 
{
  boost::this_thread::sleep(boost::posix_time::seconds(seconds)); 
}

boost::mutex mutex; 

void thread()
{
  for (int i = 0; i < 100; ++i) 
  {
    wait(1); 
    mutex.lock(); 
    std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl; 
    mutex.unlock(); 
  }
}

int main()
{
  boost::thread t1(thread); 
  boost::thread t2(thread);

  t1.join();
  t2.join();
}
  • 1 1 Answer
  • 2 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-14T23:10:02+00:00Added an answer on May 14, 2026 at 11:10 pm

    “at the same time (one after another)” is contradictory. With a call to sleep() they run at the same time. Without a call to sleep(), they run one after another. With only 100 lines to output, thread t1 completes before t2 has a change to begin execution. On my computer, I had to set your loop counter to 10000 before t1 ran long enough for t2 to launch while t1 was still executing:

    Thread 0x2305010: 0
    Thread 0x2305010: 1
    Thread 0x2305010: 2
    ...
    Thread 0x2305010: 8730
    Thread 0x2305010: 8731
    Thread 0x23052a0: 0
    Thread 0x23052a0: 1
    ...
    Thread 0x23052a0: 146
    Thread 0x23052a0: 147
    Thread 0x2305010: 8732
    Thread 0x2305010: 8733
    etc
    

    Oh, and yes, if your goal was to make the two threads take turns executing, boost::condition_variable is the solution:

    boost::mutex mutex;
    boost::condition_variable cv;
    
    void thread()
    {
      for (int i = 0; i < 100; ++i)
      {
        boost::unique_lock<boost::mutex> lock(mutex);
        std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;
        cv.notify_one();
        cv.wait(lock);
      }
      cv.notify_one();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.