Pthread in C++ Parameters
Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently.
Using pthreads, you can create a new thread and specify a function for the thread to execute. The threads can be created and managed independently of each other, and they can run concurrently, allowing your program to perform multiple tasks simultaneously.
Example 1:
Here is an example of how to create a new thread using threads in C++:
#include <pthread.h>
#include <iostream>
void *print_message_function( void *ptr );
int main() {
pthread_t thread1, thread2;
const char *message1 = "Thread 1";
const char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
if(iret1) {
std::cout<< "Error - pthread_create() return code: " << iret1 << std::endl;
exit(EXIT_FAILURE); }
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
if(iret2)
{
std::cout<< "Error - pthread_create() return code: " << iret2 << std::endl;
exit(EXIT_FAILURE);
}
std::cout<< "pthread_create() for thread 1 returns: " << iret1 << std::endl;
std::cout<< "pthread_create() for thread 2 returns: " << iret2 << std::endl;
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(EXIT_SUCCESS);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
std::cout<< message << std::endl;
}
This program creates two new threads, each executing the print_message_function. The main thread will wait for both threads to complete before exiting.
In C++, the pthread library allows you to create and manage threads.
To use pthread in your C++ program, you must include the <pthread.h> header file.
Example 2:
Here is an example of how to create a new thread using the pthread library:
#include <iostream>
#include <pthread.h>
void *thread_function(void *arg)
{
std::cout<< "Hello from a new thread!" << std::endl;
return NULL;
}
int main()
{
pthread_t thread;
int ret = pthread_create(&thread, NULL, thread_function, NULL);
if (ret != 0) {
std::cout<< "Error: unable to create thread, " << ret << std::endl;
return -1;
}
pthread_join(thread, NULL);
return 0;
}
This code creates a new thread and prints "Hello from a new thread!" to the console.
Parameters
The pthread_create function in C++ is used to create a new thread. It takes the following parameters:
- pthread_t *thread: A pointer to a pthread_t variable that will be filled in with the thread identifier of the new thread.
- constpthread_attr_t *attr: A pointer to a pthread_attr_t structure that specifies the attributes of the new thread. If this parameter is NULL, the default thread attributes will be used.
- void *(*start_routine)(void *): A pointer to the function that the new thread will execute. This function should have a prototype of void *func(void *).
- void *arg: A pointer to data that will be passed to the start routine as its argument.
Explanation:
A thread, a lightweight process, is a single flow of execution within a program. Threads are useful for parallelizing work, as they allow multiple pieces of code to be executed concurrently within a single program. pthread is a C library that provides support for creating and managing threads.
In C++, you can use the pthread library to create and manage threads by including the pthread.h header file and linking with the pthread library. The pthread_create function creates a new thread, and the pthread_join function waits for a thread to complete execution.
Example 3:
Here's an example of how to create a new thread using pthread_create:
#include <pthread.h>
void *print_message_function( void *ptr );
int main()
{
pthread_t thread1;
const char *message1 = "Thread 1";
int iret1;
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
if(iret1) {
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
exit(EXIT_FAILURE);}
pthread_join( thread1, NULL);
exit(EXIT_SUCCESS);
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
In this example, the print_message_function is the start routine that the new thread will execute. The pthread_create function takes four parameters: a pointer to a pthread_t variable that will be filled in with the thread identifier of the new thread, a pointer to a pthread_attr_t structure that specifies the attributes of the new thread, a pointer to the start routine function, and a pointer to data that will be passed to the start routine as its argument. The pthread_join function is then used to wait for the new thread to complete execution before exiting the program.