codescracker
c++

C++ Multithreading



« Previous Tutorial Next Tutorial »

Multithreading is simply a form of multitasking and a multitasking is simply the feature that allows your computer to run two or more programs at the same time.

Types of Multitasking

Basically there are the two types of multitasking:

Process-based Multitasking

Process-based multitasking handles the programs execution at the same time.

Thread-based Multitasking

Thread-based multitasking deals with the concurrent execution of peices of the same program.

C++ Multithreading Example

Here is an example program, illustrating the concept of multithreading in C++ practically.

/* C++ Multithreading - Example Program */

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<pthread.h>
void *display_hello(void *thread_id)
{
   long t_id;
   t_id=(long)thread_id;
   cout<<"Thread ID, "<<t_id<<"\n";
   pthread_exit(NULL);
}
void main()
{
   clrscr();
   pthread_t threads[5];
   int rc;
   for(int i=0; i<5; i++)
   {
      cout<<"main(): creating the thread, "<<i<<"\n";
      rc=pthread_create(&threads[i], NULL, display_hello, (void *)i);
      if(rc)
      {
         cout<<"Error: unable to create thread, "<<rc<<"\n";
         exit(-1);
      }
   }
   pthread_exit(NULL);
   getch();
}

Here is the sample output of the above C++ program:

main(): creating the thread, 0
main(): creating the thread, 1
main(): creating the thread, 2
main(): creating the thread, 3
main(): creating the thread, 4
Thread ID, 0
Thread ID, 1
Thread ID, 2
Thread ID, 3
Thread ID, 4

As you can see from the above output, this C++ program creates 5 threads with the routine pthread_create() and each thread simply prints a "Hello World." message, and then terminates with call to the pthread_exit() routine.

Let's take another C++ program, also demonstrating multithreading in C++. This example program simply shows how to pass multiple arguments via a structure.

/* C++ Multithreading - Example Program */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<pthread.h>
struct THREAD_DATA
{
   int  thrd_id;
   char *msg;
};
void *display_hello(void *thread_arg)
{
   struct THREAD_DATA *mydata;
   mydata=(struct THREAD_DATA *) threadarg;
   cout<<"Thread ID: "<<mydata->thrd_id;
   cout<<"Message: "<<mydata->msg<<"\n";
   pthread_exit(NULL);
}
void main()
{
   clrscr();
   pthread_t threads[5];
   struct THREAD_DATA td[5];
   int rc, i;
   for(i=0; i<5; i++)
   {
      cout<<"main(): creating the thread, "<<i<<"\n";
      td[i].thread_id=i;
      td[i].msg="This is the message";
      rc=pthread_create(&threads[i], NULL, display_hello, (void *)&td[i]);
      if(rc)
      {
         cout<<"Error: unable to create thread, "<<rc<<"\n";
         exit(-1);
      }
   }
   pthread_exit(NULL);
   getch();
}

Below is the sample output of this C++ program:

main(): creating the thread, 0
main(): creating the thread, 1
main(): creating the thread, 2
main(): creating the thread, 3
main(): creating the thread, 4
Thread ID: 3 Message: This is the message
Thread ID: 2 Message: This is the message
Thread ID: 0 Message: This is the message
Thread ID: 1 Message: This is the message
Thread ID: 4 Message: This is the message

Following is one more example program, illustrates how to wait for the thread completions:

/* C++ Multithreading - Example Program */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<pthread.h>
#include<dos.h>
#include<unistd.h>
void *wait(void *tm)
{
   int i;
   long tm_id;
   tm_id=(long)tm;
   sleep(1);
   cout<<"Sleeping in the thread\n";
   cout<<"Thread having id: "<<tm_id<<"  ...exiting from this\n";
   pthread_exit(NULL);
}
void main()
{
   clrscr();
   int rc, i;
   pthread_t threads[5];
   pthread_attr_t attr;
   void *status;
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
   for(i=0;i<5; i++)
   {
      cout<<"main(): creating the thread, "<<i<<"\n";
      rc=pthread_create(&threads[i], NULL, wait, (void *)i);
      if(rc)
      {
         cout<<"Error: unable to create thread, "<<rc<<"\n";
         exit(-1);
      }
   }
   pthread_attr_destroy(&attr);
   for(i=0; i<5; i++)
   {
      rc=pthread_join(threads[i], &status);
      if(rc)
      {
         cout<<"Error: unable to join, "<<rc<<"\n";
         exit(-1);
      }
      cout<<"Main: completed thread id: "<<i;
      cout<<"  exiting with status: "<<status<<"\n";
   }
   cout<<"Main: program exiting.\n";
   pthread_exit(NULL);
   getch();
}

When the above program is compile and executed, it will produce the following output:

main(): creating thread, 0
main(): creating the thread, 1
main(): creating the thread, 2
main(): creating the thread, 3
main(): creating the thread, 4
Sleeping in the thread
Thread having id: 0 .... exiting from this
Sleeping in the thread
Thread having id: 1 .... exiting from this
Sleeping in the thread
Thread having id: 2 .... exiting from this
Sleeping in the thread
Thread having id: 3 .... exiting from this
Sleeping in the thread
Thread having id: 4 .... exiting from this
Main: completed thread id: 0  exiting with status: 0
Main: completed thread id: 1  exiting with status: 0
Main: completed thread id: 2  exiting with status: 0
Main: completed thread id: 3  exiting with status: 0
Main: completed thread id: 4  exiting with status: 0
Main: program exiting.

C++ Online Test


« Previous Tutorial Next Tutorial »



© Copyright 2021. All Rights Reserved.

CodesCracker