C++11 Multithreading – Part 1 : Three Different ways to Create Thread
In this article we will learn to create threads in C++ using std::thread.
- Creating a thread using Function Pointer
- Creating a thread using Function Objects
- Creating a thread using Lambda functions
Overview C++11 Thread Library :
Original C++ only uses one single thread programming, but in 2011 a new C++11 introduced which contains new thread library.
Prerequisite compilers:
- Linux: gcc 4.8.1 (Complete Concurrency support)
- Windows: Visual Studio 2012 and MingW
Thread Creation in C++11
In C++11 in addition to default main thread we can produce additional threads by creating objects of std::thread::class.
What std::thread accepts in constructor ?
If we attach a callback with std::thread object, then that will be executed when new thread starts execution. New thread will be start after creation of object. Any thread can wait for other by calling join()
on that thread’s object.
Creating a thread using Function Pointer :
#include <iostream> #include <thread> void thr_func() { for(int x = 0; x < 100; x++); std::cout<<"thread function is running"<<std::endl; } int main() { std::thread thrObj(thr_func); for(int x = 0; x < 100; x++); std::cout<<"MainThread executing"<<std::endl; thrObj.join(); std::cout<<"End of Main function"<<std::endl; return 0; }
Output : MainThread executingthread function is running End of Main function
Creating a thread using Function Objects :
#include <iostream> #include <thread> class DisThr { public: void operator()() { for(int x = 0; x < 5; x++) std::cout<<"Display Thread"<<std::endl; } }; int main() { std::thread thrObj( (DisThr()) ); for(int x = 0; x < 5; x++) std::cout<<"Main thread"<<std::endl; std::cout<<"Waiting for thread to complete..."<<std::endl; thrObj.join(); std::cout<<"Main Thread completes execution"<<std::endl; return 0; }
Output : Main threadDisplay Thread Display Thread Display Thread Display Thread Display Thread Main thread Main thread Main thread Main thread Waiting for thread to complete... Main Thread completes execution
Creating a thread using Lambda functions :
#include <iostream> #include <thread> int main() { int a = 9; std::thread thrObj([]{ for(int x = 0; x < 5; x++) std::cout<<"Display Thread"<<std::endl; }); for(int x = 0; x< 5; x++) std::cout<<"Main Thread"<<std::endl; thrObj.join(); std::cout<<"Main Thread completes execution"<<std::endl; return 0; }
Output : Main ThreadDisplay Thread Display Thread Display Thread Display Thread Main Thread Main Thread Main Thread Main Thread Display Thread Main Thread completes execution
Differentiating between threads
Each std::thread object has an associated ID and we can fetch it using following syntax: std::thread::get_id().
#include <iostream> #include <thread> void thr_func() { std::cout<<"Inside Thread that has ID = "<<std::this_thread::get_id()<<std::endl; } int main() { std::thread thrObj1(thr_func); std::thread thrObj2(thr_func); if(thrObj1.get_id() != thrObj2.get_id()) std::cout<<"IDs of both threads are different"<<std::endl; std::cout<<"ID of Thread1 in main thread = "<<thrObj1.get_id()<<std::endl; std::cout<<"ID of Thread2 in main thread = "<<thrObj2.get_id()<<std::endl; thrObj1.join(); thrObj2.join(); return 0; }
Output : IDs of both threads are different ID of Thread1 in main thread = 140250265655040 ID of Thread2 in main thread = 140250257262336 Inside Thread that has ID = 140250265655040 Inside Thread that has ID = 140250257262336