C++ Multithreading

In this C++ tutorial, let us discuss multithreading concepts and creation or termination of threads with an example program.

Introduction of Multithreading

Multithreading is a specific form of multitasking that allows your computer to run two or more programs concurrently. There are two types of multitasking such as

  • process-based multitasking
  • thread-based multitasking

The Process-based multitasking controls the concurrent execution of programs, whereas the Thread-based multitasking deals with the concurrent execution of parts of the same program.

In general, C++ does not hold any built-in functions for multithreaded applications. Instead, it relies entirely upon the operating system to provide that feature.

Creation of Thread

The routine given below is used to create a POSIX thread. 

#include <pthread.h>
pthread_create (thread, attr, start_routine, arg) 

Here, pthread_create creates a new thread and makes it executable. This routine can be called several numbers of times from anywhere within the code.

Description of parameters in the above-specified routine

  • thread

An opaque, unique identifier for the new thread returned by the subroutine.

  • attr

An opaque attribute object that may be used to set thread attributes. You can specify a thread attributes object or NULL for the default values.

  • start_routine

The C++ routine that the thread will execute once it is created.

  • arg

A single argument that may be passed to start_routine. It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed.

Termination of Threads

 The routine given below is used to terminate a POSIX thread 

#include <pthread.h>
pthread_exit (status) 

Here pthread_exit is used to explicitly exit a thread. Typically, the pthread_exit() routine is called after a thread has completed its work and is no longer required to exist.

C++ Program for creation and termination of threads

This following example code creates 5 threads with the pthread_create() routine and then terminates it using pthread_exit().

#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
#define NUM_THREADS 5
void *PrintHello(void *threadid) {
   long tid;
   tid = (long)threadid;
   cout << "Hello World! Thread ID, " << tid << endl;
   pthread_exit(NULL);
}
int main () {
   pthread_t threads[NUM_THREADS];
   int rc;
   int i;   
   for( i = 0; i < NUM_THREADS; i++ ) {
      cout << "main() : creating thread, " << i << endl;
      rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);
            if (rc) {
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

Output

main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Hello World! Thread ID, 0
Hello World! Thread ID, 1
Hello World! Thread ID, 2
Hello World! Thread ID, 3
Hello World! Thread ID, 4

Leave a Comment

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