c++ Thread Example

Spread the love

In this c++ thread example, we wrap a class object in a thread and execute it while running a loop from the main thread.

std::thread myThread(ShowMeTheThread());

myThread.join() //

Why wrap a process or class in a c++ thread?

Generally, we would run a method or class in a thread when it either has a long running task or when something is going to probably stop it which if it were run in the main task, would cause everything to stop.

A running c++ thread process that stops often:

That would be a thread that had been sent off to retrieve some i/o from somewhere. Perhaps the object needs to read or write to a stream such as a slow or busy rest api or a website that might throw a 404 after a few tens of seconds or a busy socket server like a chat room that sporadically gets filled and can’t easily handle more connections.

There are quite a few reasons for a c++ thread to become blocked. So, its better that a single individual c++ thread blocks rather than your whole process which might just have other work it can be doing from its main thread.

#include <iostream>
#include <thread>

class ShowMeTheThread
{
public:
    void operator()()     
    {
        for(int i = 0; i < 200; i++)
            std::cout<<"In ShowMeTheThread Executing"<<std::endl;
    }
};
 
int main()  
{
    std::thread threadObj( (ShowMeTheThread()) );
    for(int i = 0; i < 100; i++)
        std::cout<<"Display From Main Thread "<<std::endl;
    std::cout<<"Waiting For ShowMeTheThread to complete"<<std::endl;
    threadObj.join();
    std::cout<<"Exited from Main Thread"<<std::endl;
    return 0;
}

In the above example, we ask our c++ threadObj to encapsulate the ShowMeTheThread class and run it separately from the primary c++ thread that runs the main() class.

You can tell from the output that both c++ threads are running because their outputs are slightly different and you can see their print outputs right in the code sample below. One says “Display From Main Thread” and the other says “ShowMeTheThread Executing” c++ thread tutorial with sample codes

And eventually the main thread writes — Waiting for ShowMeTheThread to complete while ShowMeTheThread is still executing

There are quite a few ways of adding c++ thread constructs to your code. Don’t stop with just this example, jump in and find some more.

You could look for more information about c++ threads here

The sample code below came from the link above

// thread example
#include <iostream>       // std::cout
#include <thread>         // std::thread
 
void foo() 
{
  // do stuff...
}

void bar(int x)
{
  // do stuff...
}

int main() 
{
  std::thread first (foo);     // spawn new thread that calls foo()
  std::thread second (bar,0);  // spawn new thread that calls bar(0)

  std::cout << "main, foo and bar now execute concurrently...\n";

  // synchronize threads:
  first.join();                // pauses until first finishes
  second.join();               // pauses until second finishes

  std::cout << "foo and bar completed.\n";

  return 0;
}

Another good source for learning about multithread c++ code can be found over here at Hacker noon.

Go back to the top of this document

Top