Tuesday 18 October 2016

Step by step multithreading : exceptions and threading.


First read below mentioned program -


#include <iostream>
#include <thread>
using namespace std;
void hello(); void foo() { thread t1(hello); //Do some work here, implement your business logic; t1.join(); } /* What if you get some exception in the function foo, before it join the thread t1. In that case thread will be disjoint. */ int main(int argc, char **argv) { foo(); return 0; }

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

As application developer you knew there is a possibility that any function who implements business logic can throw exceptions. What If function foo() throws any exception, before control reaches to t1.join(); ?

Then child thread t1 will never join parent thread. Which is not, what we want. We can solve this problem very well through try-catch blog (exception handling).

#include <iostream>
#include <thread>
using namespace std;
void hello(); void foo() { thread t1(hello); try { //Do some work here; } catch(...) { t1.join(); throw; } t1.join(); } /* we can solve that issue, with try catch block. */ int main(int argc, char **argv) { foo(); return 0; }

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

If any exception occurs, first we will join the thread in catch block, then throw the exception. By doing this we will achieve our intention of joining the child thread to parent thread.

Don't you feel doing this in every function, where you want to implement the thread, will become costly operation? As exception handling has a cost to implement, and your program will become slower compare to the version where you were not using exception handling.

Wait, think a second. Don't you feel that, you have seen this issue before? Yes, while dealing with pointers in C++. What if exception come and control don't reach to delete statement, then you will be facing memory leak issue as C++ don't provide garbage collector.

Remember, pointer issue? And how smartly we had solved that issue, by using smart pointers. Who will clean itself, if it goes out of control. Yes I am talking about RAII, which will help C++ developer to avoid memory leak. 

Look at the below mentioned program -
#include <iostream>
#include <thread>
using namespace std;
void hello(); /* we can also solve that issue by using RAII. crate a Wrapper class, in the destructor of Wrapper class join the thread.so as soon as it goes out of control it will call destructor and then join the thread. */ // a dummy class class ThreadWrapper { public: ThreadWrapper(thread& t); ~ThreadWrapper() { t.join(); } }; void foo() { //use RAIT ThreadWrapper w(thread t1(hello)); //Do some work here, implement your business logic; // now no need to call join here, as ThreadWrapper's // destructor will call it, as soon as it goes out of control. } int main(int argc, char **argv) { foo(); return 0; } void hello() { cout<<"welcome to C++ multithreading"; }

Note: this program is to understand the concept, actual ThreadWrapper class will have many other function in it, understand the intent right now, I will post full working code in some other blog.

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++.

No comments:

Post a Comment