- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Multithreading
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
- thread-based
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.
« Previous Tutorial Next Tutorial »