C++ Multithreading

Introduction to C++ Multithreading

एक thread किसी process में stream होती है जिससे कोई task perform होता है। किसी process में एक या एक से अधिक threads हो सकती है। हर thread एक independent task को represent करती है।

ऐसी process जिनमें सिर्फ एक thread होती है single threaded process कहलाती है। ऐसी processes से एक साथ एक से अधिक tasks perform नहीं किये जा सकते है। उदाहरण के लिए calculator एक single threaded process होती है। एक calculator में आप एक साथ कई अलग अलग calculations perform नहीं कर सकते है।

जिन processes में एक से अधिक threads होती है वे multithreaded processes कहलाती है। Multithreaded processes में अलग अलग threads के द्वारा एक साथ कई tasks perform किये जा सकते है।

उदाहरण के लिए आपका browser एक multithreaded process है उसमे open किया गया हर tab एक thread (या stream) को represent करता है। उन tabs के द्वारा एक साथ अलग अलग webpages को देखा जा सकता है।

जब कई threads एक साथ कार्य करती है तो वह process multithreading कहलाती है। जब आप कोई C++ program create करते है तो वह एक single threaded program होता है। आपके program का main() method एक single thread को represent करता है।

लेकिन आप अपने program को multithreaded बना सकते है और एक साथ कई tasks perform कर सकते है।

C++ में complete multithreading support C++ के version 11 में add किया गया था। इससे पूर्व multithreading को implement करने के लिए आपको C language में available POSIX threads (pthreads) library को use करना होता था।

हालाँकि POSIX threads library द्वारा multithreading implement की जा सकती थी। लेकिन यह कोई standard तरीका नहीं था और इसे implement करना बहुत complex था।

C++ के version 11 में आपको <thread.h> header file provide की गयी है जिसमे multithreading के लिए जरुरी thread class और functions available है जिन्हें आप अपने program में कँही भी use कर सकते है।

Implementing Multithreading in C++

किसी भी C++ program में multithreading implement करने के लिए सबसे पूर्व आप <thread.h> header file को अपने program में include करते है। इसका syntax निचे दिया जा रहा है।

#include<thread>

OR

#include<thread.h>

केवल <thread.h> header file को include करने के बाद ही आप अपने program में threads create कर सकते है।

Creating Threads

ऊपर बताई गयी <thread.h> header file में available thread class एक single thread को represent करती है। एक thread create करने के लिए आपको बस thread class का object create करना होता है।

इस प्रकार आप thread class के कई objects create करके कई threads create कर सकते है। इसका syntax निचे दिया जा रहा है।

thread thread-object-name(callable-code);

जैसा की आप ऊपर दिए गए syntax में देख सकते है thread class का object create करते समय callable code pass किया जाता है। Callable code वह code होता है जो thread के launch होने पर execute किया जाता है।

Callable code निचे दिए जा रहे codes में से कोई एक हो सकता है।

  • Function - Callable code के रूप में आप किसी function का नाम pass कर सकते है। वह function thread launch होने पर execute हो जाता है।
  • Class object - Callable code के रूप में किसी class का object भी pass किया जा सकता है। ऐसा करने के लिए आपको उस class में () operator को overload करना होता है। इस operator को override करते समय आप जो code define करते है वही code thread के launch होने पर call होता है।
  • Lambda expression - Callable code के रूप में lambda expression को भी pass किया जा सकता है। Lambda expression एक unnamed function ही होता है। Lambda function के द्वारा आप thread create करते समय ही उसका callable code भी define कर पाते है।

यदि callable code किसी प्रकार का argument accept करता है तो arguments को आप callable code के बाद comma से separate करके लिखते है। इसका syntax निचे दिया जा रहा है।

thread thread-object-name(callable-code,arg1,arg2,....argN);

हालाँकि lambda expression के case में इस प्रकार arguments नहीं pass किये जाते है।

Joining Threads

Thread object create करने के बाद thread को main thread के साथ attach (join) किया जाता है। यदि thread को main thread के साथ join नहीं किया जाए तो आपकी main thread का execution पहले complete हो जाएगा और thread object द्वारा define की गयी thread execute ही नहीं हो पाएगी।

किसी thread को main thread के साथ join करने के लिए join() method का प्रयोग किया जाता है। इसका general syntax निचे दिया जा रहा है।

thread-object-name.join();

एक बार जब किसी thread को main thread के साथ join कर दिया जाता है तो main thread अपना execution complete करके terminate होने से पहले उस thread के लिए wait करती है।

Detaching Threads

जिस प्रकार threads को attach (join) किया जाता है उसी प्रकार उन्हें detach भी किया जाता है। इसके लिए detach() function का प्रयोग किया जाता है। इस function को thread object द्वारा call किया जाता है।

यह function किसी thread को उसकी parent thread से detach करता है। इसका general syntax निचे दिया जा रहा है।

thread-object-name.detach();

एक thread detach होने के बाद joinable बन जाती है उसे दूसरी threads के साथ attach किया जा सकता है।

Example of C++ Multithreading

C++ multithreading के use को निचे उदाहरण द्वारा समझाया जा रहा है।

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

void myFunc()
{
    cout<<"T1 thread executed"<<endl;
}

int main()
{
    thread t1(myFunc);
    t1.join();
    cout<<"Main thread executed"<<endl;
    return 0;
}

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

T1 thread execute
Main thread executed