Multithreading in C++

Multithreading is an capability of a platform i.e.Operating System, Virtual Machine  etc.or application to generate a process that contains of numerous threads of execution (threads). A thread of implementation is the smallest order of programming instructions that can be managed self-sufficiently by a scheduler. These threads can run parallel in addition to it can surge efficiency of programs.

In Multicore and Multiprocessor systems multithreading means that dissimilar threads are performed at the equal time on unlike cores or processors.

For single core systems multithreading distributes the time between the threads. The operating system in turn refers a definite number of instructions from each thread to the processor. Threads are not performedinstantaneously. Operating System only pretends their simultaneous execution. This feature of the operating system is termed multithreading.

Multithreading is used when the parallel execution of certain tasks clues to a more well-organized use of resources of the system.

Forming threads

  • First, you have to take in thread header in your program:
  • When you wish to create a thread, you have to make an object of a thread
//this thread does not signify any thread of execution
  • if default constructor of thread class is used, we do not pass any data to the thread. that nothing is implemented in this thread. We have to reset a thread. It can be finished in different ways.Ending threads Contrasting some other executions of threads, C++ destructors for automatic objects are permitted to run in a well defined and steady manner when a thread is completed.The following list comprises some of the reasons of thread termination:
    • A thread calls pthread_exit() or else returns from the thread twitch routine.
    • A thread is the goal of pthread_cancel().
    • A thread is concluded due to an unhandled exception.
    • A thread is in a process that comprisesadded thread that calls exit() or abort().
    • A thread is in a process that is completed by the system administrator.
    • A thread is being dismissed by the system administrator.

    When a thread ends, the following happens:

    1. If the thread was completed using pthread_exit(), pthread_cancel() or else return from the thread start routine, then removal cleanup handlers and data destructors are run.
    2. The thread is finished. At the time that the thread is completed, both C++ destructors for spontaneous objects and IBM®i cancel handlers run.


 Passing Arguments en route for Threads

In the earlier examples, we used only functions and objects devoid of passing any arguments to these functions and objects.

Here,We can use a function by parameters for thread initialization. Generate new function for testing this option:


here, this function proceeds three arguments. If you want to set a thread with this function, first you have to pass a pointer to this function, then pass the arguments to the function in the similar order as they are in the parameter list of the function:


When you need to make ready a thread with an object with parameters, we have to add consistent parameter list to the overloading version of operator ():


here, operator () takes two parameters:


voidoperator()(int*arr,int length)

The initialization of the thread with an object in this case is alike to using a function with parameters:


It is conceivable to use a member function of a class to pass parameters to thread. Improve new public function to myFunctorParam class:


Passing arguments towards member function:

  • When you pass arguments to the member function of a class, you have to identify arguments in the identical order as they are itemized in the parameter list of the function. It is completed after the second parameter of the thread constructor:
thread test2(&myFunctorParam::changeSign,&objParamPass,arr2,5);

Joining and Separating Threads
Thread joining is completed by using join() member function of a thread class:

void join();

This function returns only later all the threads are finished. i.e. that the main thread will break until child thread does not finish its execution:
Call join() for the thread, made in the previous example and run the program again:


  • here, thread converts not joinable afterdetach() member function is called:
void detach()

• This function removes a thread from the parent thread. It permits parent and child threads to be executed individualistically from each other. After the call of detach() function, the threads are not coordinated in any way:

You will notice that main thread is not coming up for the expiry of its child thread.

Leave A Comment

Your email address will not be published. Required fields are marked *