389

Can someone post a simple example of starting two (Object Oriented) threads in C++.

I'm looking for actual C++ thread objects that I can extend run methods on (or something similar) as opposed to calling a C-style thread library.

I left out any OS specific requests in the hopes that whoever replied would reply with cross platform libraries to use. I'm just making that explicit now.

1

7 Answers 7

716

Create a function that you want the thread to execute, for example:

void task1(std::string msg)
{
    std::cout << "task1 says: " << msg;
}

Now create the thread object that will ultimately invoke the function above like so:

std::thread t1(task1, "Hello");

(You need to #include <thread> to access the std::thread class.)

The constructor's first argument is the function the thread will execute, followed by the function's parameters. The thread is automatically started upon construction.

If later on you want to wait for the thread to be done executing the function, call:

t1.join();

(Joining means that the thread who invoked the new thread will wait for the new thread to finish execution, before it will continue its own execution.)


The Code

#include <string>
#include <iostream>
#include <thread>

using namespace std;

// The function we want to execute on the new thread.
void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
    // Constructs the new thread and runs it. Does not block execution.
    thread t1(task1, "Hello");

    // Do other things...

    // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
    t1.join();
}

More information about std::thread here

  • On GCC, compile with -std=c++0x -pthread.
  • This should work for any operating-system, granted your compiler supports this (C++11) feature.
20
  • 5
    @Preza8 VS10 does not support many features in C++11. VS12 and VS13 support thread.
    – jodag
    Commented Jan 9, 2014 at 0:53
  • 4
    In the comment "GCC versions below 4.7, use -std=c++0x (instead of -std=c++0x)" I believe the second "c++0x" should instead be "c++11", but that's not possible to change because the edit is too small.
    – zentrunix
    Commented Mar 25, 2014 at 16:19
  • 1
    @MasterMastic What difference does it make if instead of std::thread t1(task1, "Hello") we use std::thread t1(&task1, "Hello"), passing the reference of the function? Should we declare the function as a pointer in this case? Commented Nov 4, 2014 at 16:32
  • 3
    @user2452253 If you pass "task1" you pass a pointer to the function you want to execute (which is good). If you pass "&task1" you pass a pointer to a pointer of a function, and the results would probably not be so pretty and very undefined (it would be like trying to execute random instructions one after another. With the right probability you just might open the gateway to hell). You really just want to pass the function pointer (a pointer with the value of the address where the function begins). If that doesn't clear things up, let me know, and best of luck! Commented Nov 4, 2014 at 16:47
  • 2
    @curiousguy Well, the right way to do it is to pass a pointer of the function you want to run. In this case the function is task1. So the function pointer is also denoted by task1. But thank you for bringing this up because I believe I was wrong and it's equivalent to &task1, so it doesn't matter which form you choose to write (if so, I guess that pointer to pointer of the function is &&task1 -- that would be bad). Relevant Question. Commented Jul 5, 2018 at 9:22
85

Well, technically any such object will wind up being built over a C-style thread library because C++ only just specified a stock std::thread model in C++0x, which was just nailed down and hasn't yet been implemented.

The problem is somewhat systemic. Technically the existing C++ memory model isn't strict enough to allow for well-defined semantics for all of the 'happens before' cases. Hans Boehm wrote an paper on the topic a while back and was instrumental in hammering out the C++0x standard on the topic.

Threads Cannot be Implemented as a Library

That said, there are several cross-platform thread C++ libraries that work just fine in practice. The Intel thread building blocks contains a tbb::thread object that closely approximates the C++0x standard and Boost has a boost::thread library that does the same.

Using boost::thread, you'd get something like:

#include <boost/thread.hpp>

void task1() {
    // do stuff
}

void task2() {
    // do stuff
}

int main (int argc, char ** argv) {
    using namespace boost;
    thread thread_1 = thread(task1);
    thread thread_2 = thread(task2);

    // do other stuff
    thread_2.join();
    thread_1.join();
    return 0;
}
4
  • 10
    Boost thread is great - my only problem was that you couldn't (when I last used it) actually access the native underlying thread handle as it was a private class member! There's a TON of stuff in win32 that you need the threadhandle for, so we tweaked it to make the handle public. Commented Nov 5, 2008 at 19:40
  • 5
    Another problem with boost::thread is that as I recall you don't have the freedom to set the stack size of the new thread -- a feature that is also lamentably not included in c++0x standard. Commented Nov 5, 2008 at 20:13
  • That code gives me a boost::noncopyable exception for this line: thread thread_1 = thread(task1); Maybe it's because I'm using an older version (1.32).
    – Frank
    Commented Feb 21, 2009 at 0:42
  • task1 was not declared in this scope? Commented Mar 22, 2019 at 20:45
31
#include <thread>
#include <iostream>
#include <vector>
using namespace std;

void doSomething(int id) {
    cout << id << "\n";
}

/**
 * Spawns n threads
 */
void spawnThreads(int n)
{
    std::vector<thread> threads(n);
    // spawn n threads:
    for (int i = 0; i < n; i++) {
        threads[i] = thread(doSomething, i + 1);
    }

    for (auto& th : threads) {
        th.join();
    }
}

int main()
{
    spawnThreads(10);
}
1
  • 3
    An explanation would be in order. E.g., what is the idea/gist? What version of C++ is assumed? What system/compiler (incl. versions) was it tested with? From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Commented May 12, 2022 at 21:40
25

When searching for an example of a C++ class that calls one of its own instance methods in a new thread, this question comes up, but we were not able to use any of these answers that way. Here's an example that does that:

Class.h

class DataManager
{
public:
    bool hasData;
    void getData();
    bool dataAvailable();
};

Class.cpp

#include "DataManager.h"

void DataManager::getData()
{
    // perform background data munging
    hasData = true;
    // be sure to notify on the main thread
}

bool DataManager::dataAvailable()
{
    if (hasData)
    {
        return true;
    }
    else
    {
        std::thread t(&DataManager::getData, this);
        t.detach(); // as opposed to .join, which runs on the current thread
    }
}

Note that this example doesn't get into mutex or locking.

3
  • 10
    Thanks for posting this. Note that the general form of invoking a thread on instance method goes something like this: Foo f; std::thread t(&Foo::Run, &f, args...); (where Foo is a class that has 'Run()' as a member function).
    – Jay Elston
    Commented Jan 8, 2019 at 19:44
  • 8
    Thanks for posting this. I seriously don't understand why all threading examples use global functions. Commented Feb 15, 2020 at 13:00
  • Change this to shared_from_this if current Class is derived from enable_shared_from_this.
    – Eugene
    Commented Feb 1, 2021 at 15:39
22

There is also a POSIX library for POSIX operating systems.

Check for compatibility:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>

void *task(void *argument){
    char* msg;
    msg = (char*)argument;
    std::cout << msg << std::endl;
}

int main(){
    pthread_t thread1, thread2;
    int i1, i2;
    i1 = pthread_create(&thread1, NULL, task, (void*) "thread 1");
    i2 = pthread_create(&thread2, NULL, task, (void*) "thread 2");

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    return 0;
}

Compile with -lpthread.

POSIX Threads

2
  • Re "Compile with -lpthread": What compiler? GCC? Commented May 12, 2022 at 21:48
  • 3
    Totally outdated answer; threads where added to C++11 and that is what one should use now. NOT posix threads.
    – Carlo Wood
    Commented May 25, 2022 at 13:09
18

Unless one wants a separate function in the global namespace, we can use lambda functions for creating threads.

One of the major advantage of creating a thread using lambda is that we don't need to pass local parameters as an argument list. We can use the capture list for the same and the closure property of lambda will take care of the lifecycle.

Here is sample code:

int main() {
    int localVariable = 100;

    thread th { [=]() {
        cout << "The value of local variable => " << localVariable << endl;
    }};

    th.join();

    return 0;
}

By far, I've found C++ lambdas to be the best way of creating threads especially for simpler thread functions.

2
10

It largely depends on the library you decide to use. For instance, if you use the wxWidgets library, the creation of a thread would look like this:

class RThread : public wxThread {

public:
    RThread()
        : wxThread(wxTHREAD_JOINABLE){
    }
private:
    RThread(const RThread &copy);

public:
    void *Entry(void){
        //Do...

        return 0;
    }

};

wxThread *CreateThread() {
    //Create thread
    wxThread *_hThread = new RThread();

    //Start thread
    _hThread->Create();
    _hThread->Run();

    return _hThread;
}

If your main thread calls the CreateThread method, you'll create a new thread that will start executing the code in your "Entry" method. You'll have to keep a reference to the thread in most cases to join or stop it.

More information is in the wxThread documentation.

2
  • 1
    You forgot to delete the thread. Perhaps you meant to create a detached thread?
    – aib
    Commented Nov 5, 2008 at 18:57
  • 1
    Yup right, I extracted the code from some code I'm currently working on and of course the reference to the thread is stored somewhere in order to join, stop and delete it later on. Thanks. :)
    – LorenzCK
    Commented Nov 6, 2008 at 16:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.