The concept of c++ multithreading is to be able to run two or more programs at the same time. A thread of execution is the smallest sequence of programming instructions, which can be managed independently by a scheduler. These threads can parallel and it can increase the efficiency of programs. Processors with multiple cores mean that the different threads are executed at the same time on different cores of the processor.

220px-Multithreaded_process.svg

Multithreading diagram containing a process with two threads of execution, running on a single processor

In order to run the multithreading code examples below, you will need the C POSIX library installed as we are using the POSIX library for multithreading support.

Creating threads in c++ multithreading:

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Joining and detaching threads
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
#define NUM_THREADS 5
 
void *wait(void *t)
{
int i;
long tid;
 
tid = (long)t;
 
sleep(1);
cout << "Sleeping in thread\n";
cout << "Thread with id : " << tid;
pthread_exit(NULL);
}
 
int main ()
{
int rc;
int i;
pthread_t threads[NUM_THREADS];
pthread_attr_t attr;
void *status;
 
// Initialize and set thread to be joinable
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
for( i=0; i < NUM_THREADS; i++ )
{
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, wait, (void *)i );
if (rc)
{
cout << "Error:unable to create thread\n" << rc
exit(−1);
}
}
 
// free attribute and wait for the other threads
pthread_attr_destroy(&attr);
for( i=0; i < NUM_THREADS; i++ )
{
rc = pthread_join(threads[i], &status);
if (rc)
{
cout << "Error:unable to join\n" << rc <<
exit(−1);
}
cout << "Main: completed thread id :" << i ;
cout << " exiting with status\n" << status;
}
 
cout << "Main: program exiting\n";
pthread_exit(NULL);
}

 

Passing Arguments to threads in c++ multithreading:

 

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Passing arguments to threads
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
#define NUM_THREADS 5
 
struct thread_data
{
int thread_id;
char *message;
};
 
void *PrintMessage(void *threadarg)
{
struct thread_data *my_data;
 
my_data = (struct thread_data *) threadarg;
 
cout << "Thread ID : " << my_data−>thread_id ;
cout << " Message : " << my_data−>message << endl;
 
pthread_exit(NULL);
}
 
int main ()
{
pthread_t threads[NUM_THREADS];
struct thread_data td[NUM_THREADS];
int rc;
int i;
 
for( i=0; i < NUM_THREADS; i++ )
{
cout <<"main() : creating thread, " << i << endl;
td[i].thread_id = i;
td[i].message = "This is message";
rc = pthread_create(&threads[i], NULL, PrintMessage, (void *)&td[i]);
if (rc)
{
cout << "Error:unable to create thread\n" << rc;
exit(−1);
}
}
pthread_exit(NULL);
}
Share this post

FAQ's

What is multithreading in c++?

Multithreading in C++ is the concept of being able to run two or more programs at the same time.

How do we create threads in c++?

In order to create threads in C++, you will need the C POSIX library installed as we are using the POSIX library for multithreading support in C++