Wednesday, 12 October 2016

Step by step multithreading : Join()

C++ 11 has introduced multithreading in standard library. Let us see some basic concepts and learn threading.

First look at the below mentioned program -

#include <iostream>
#include <thread>
using namespace std;

void hello();

int main(int arvc, char **argv)
{
    thread t1(hello);
    t1.join(); // join means, main thread will wait for t1 to complete.
    return 0;
}

void hello()
{
    cout<<"welcome to C++ multithreading";
}

/*
compilation command -
    g++ -std=c++11 program.cpp

output -
    welcome to C++ multithreading
*/

Note: you have to compile this program through C++11 compiler.

Here, we have two new things to notice.
  1. #include <thread> // which is added in C++ 11 standard library. To achieve multithreading in C++.
  2. We have passed a function (here function is hello()) to the thread object. 
  3. And last but not least Join() , see the description below.

Join - 

This is a function of thread class, which will tell the main thread, wait until your child thread gets completed, after child thread is completed then only main thread will start, and then execute further instructions.


To understand more just see another easy example (focus on the output), in below mentioned example, first main thread waits for child thread (t01) to complete, then control goes to next statement, then it again waits for child thread (this time t02) to complete, and then finishes the program. 


#include <iostream>
#include <thread>
using namespace std;

void hello_by_thread01();
void hello_by_thread02();

int main(int arvc, char **argv)
{
    thread t01(hello_by_thread01);
    t01.join(); // join means, main thread will wait for t01 to complete.
    cout<< endl << "now in main thread from thread 01";
    
    thread t02(hello_by_thread02);
    t02.join(); // join means, main thread will wait for t02 to complete.
    cout<< endl << "now in main thread from thread 02";
    
    return 0;
}

void hello_by_thread01()
{
    cout<<"welcome to C++ multithreading, through thread 01";
}
void hello_by_thread02()
{
    cout<<endl<<"welcome to C++ multithreading, through thread 02";
}
/*
compilation command -
    g++ -std=c++11 FileName.cpp

output -
$ ./a.exe
welcome to C++ multithreading, through thread 01
now in main thread from thread 01
welcome to C++ multithreading, through thread 02
now in main thread from thread 02
*/

Thanks for reading this, please write questions or some more useful information related to this topic on comment section. To learn more about threading, see the full learning index here, or join multithreading learning page on FB or on G++.

Wednesday, 16 December 2015

Multithreading, basics part 1.

Background -

Before we jump into multi threading, let us see it's background.

During initial days of programming, programmer never thought of parallel programming. We were more depend on the speed of processors. From programming skills point of view, we were more focused on using good data structures in our programs with optimized time and space complexity, then writing parallel programs. 

Performance of the processors were increasing every year. Thanks to AMD, Intel, Nvidia and other chip maker who has been developing small but fast processors year by year. How we were able to achieve better performance consistently year by year? because of Moore's law, but soon after processors hit the performance wall, and couldn't go further in terms of performance, with single core design.



Below is the block diagram of single core -




Then chip maker companies decided to go with multiple core in one processor, and invented dual core processors first. After hardware industry discover multi core in a single chip, Operating systems were the first software who utilized it properly and come up with fast OS, but application development were not fully migrated to parallel programming or multithreading.




block diagram of multi core processors -




Speed/performance has been always a big plus point of any software product. Now a days we are talking about - complex big data problems, machine learning algorithm, data mining algos of large data. We have to come up with parallel programming skill set where we can actually utilize the power of multi core processors. 

Running single threaded application on multi core processor has no big benefits. Writing your programs knowing the latest hardware architecture will make it even faster.