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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:56:30+00:00 2026-05-22T23:56:30+00:00

In Java, I would do something like: Thread t = new MyThread(); t.start(); I

  • 0

In Java, I would do something like:

Thread t = new MyThread();
t.start();

I start thread by calling start() method. So later I can do something like:

for (int i = 0; i < limit; ++i)
{
    Thread t = new MyThread();
    t.start();
}

To create a group of threads and execute the code in run() method.

However, in C++, there’s no such thing as start() method. Using Boost, if I want a thread to start running, I have to call the join() method in order to make a thread running.

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

class Worker
{
public:
    Worker() 
    {
        // the thread is not-a-thread until we call start()
    }

    void start(int N)
    {
        m_Thread = boost::thread(&Worker::processQueue, this, N);
    }

    void join()
    {
        m_Thread.join();
    }

    void processQueue(unsigned N)
    {
        float ms = N * 1e3;
        boost::posix_time::milliseconds workTime(ms);

        std::cout << "Worker: started, will work for "
                  << ms << "ms"
                  << std::endl;

        // We're busy, honest!
        boost::this_thread::sleep(workTime);
        std::cout << "Worker: completed" << std::endl;
    }

private:

    boost::thread m_Thread;
};

int main(int argc, char* argv[])
{
    std::cout << "main: startup" << std::endl;

    Worker worker, w2, w3, w5;

    worker.start(3);
    w2.start(3);
    w3.start(3);
    w5.start(3);

    worker.join();
    w2.join();
    w3.join();
    w5.join();
    for (int i = 0; i < 100; ++i)
    {
        Worker w;
        w.start(3);
        w.join();
    }
    //std::cout << "main: waiting for thread" << std::endl;    

    std::cout << "main: done" << std::endl;

    return 0;
}

On the code above, the for loop to create 100 threads, normally I must use a boost::thread_group to add the thread function, and finally run all with join_all(). However, I don’t know how to do it with thread function putting in a class which uses various class members.

On the other hand, the loop above will not behave like the loop in Java. It will make each thread execute sequentially, not all at once like the other separated threads, whose own join() is called.

What is join() in Boost exactly? Also please help me to create a group of threads which share the same class.

  • 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-22T23:56:31+00:00Added an answer on May 22, 2026 at 11:56 pm

    join doesn’t start the thread, it blocks you until the thread you’re joining finishes. You use it when you need to wait for the thread you started to finish its run (for example – if it computes something and you need the result).

    What starts the thread is boost::thread, which creates the thread and calls the thread function you passed to it (in your case – Worker::processQueue).

    The reason you had a problem with the loop is not because the threads didn’t start, but because your main thread didn’t wait for them to execute before finishing. I’m guessing you didn’t see this problem in Java because of the scheduling differences, aka “undefined behavior”. after edit In Java the threading behaves slightly differently, see the comment below for details. That explains why you didn’t see it in Java.

    Here’s a question about the boost::thread_group. Read the code in the question and the answers, it will help you.

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

Sidebar

Related Questions

I am using java to create an application for network management. In this application
I have a Java application that uses the Executor framework and I have code
Is there a pre-implemented linked list out there that would have been implemented using
I'm using an opaque library which I'm not sure can work on appengine. I'm
I'm moving from C# to Java, and I need to implement a set of
Hey all, this will contain a few questions since I don't seem to really
I'm working on an application which uses servlets to get and set data for
Hey guys, I am reading these so called non-blocking techniques, but i have few
I am in the process of writing a web-app that uses multiple web APIs.
Well I know that selenium world is full of file upload threads, and this

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.