In this article, we will discuss the pthreadcondbroadcast method in C++. This method belongs to the POSIX library. This function is specifically used in the context of intra-thread UI development. The pthreadcondbroadcast function has a real concept that should be understood using multi-threads, conditions, and the principles of the pthread library.
What is Thread?
The basic unit of a process is a thread, which is the smallest bit of work being done in a process. The strings residing in the same process also use the same resources, such as memory space, file descriptors, process-specific attributes, and other process-specific elements. Threading enables multi-tasking, which produces parallel execution. Thus, overall program efficiency is increased.
Condition Variables:
Condition variables are synchronization mechanisms that multithreaded programs use to enable threads to exchange vital information and carry out the execution process in a timely manner. These variables are used along with the mutex variables.
Pthreads Library:
The Pthreads (POSIX) or portability of the Unix-like operating system for threading can be made by API (Application Programming Interface), which is standardized for Unix systems, including Linux and macOS. It consists of an interface and method set for creating, performing operations, and synchronizing threads.
Mutexes:
Mutexes or mutual exclusion are used to protect critical sections of code that require shared resources to be accessed by a single thread at a time. Before a thread can access the critical section that a mutex has locked, the thread has to lock the mutex first.
pthread_cond_broadcast:
The pthreadcondbroadcast function helps signal multiple threads which are been waiting stage. Instead of unblocking only those threads, a condition change triggers a pthreadcondbroadcast that unblocks all of them at once. This activity will bypass waiting threads that are holding on to a condition variable.
The pthreadcondbroadcast function will unblock all the threads blocked by the semaphore cond. The pthreadcondsignal function will unblock at least one of the threads waiting on the specified condition variable cond if any threads are currently waiting on it. When multiple threads are blocked in a condition variable. The unblocking will be determined by the scheduling policy. The status of each thread is changed to the running state after a return pthreadcondwait or pthreadcondtimedwait result of pthreadcondbroadcast or pthreadcondsignal. The runnable thread will own the mutex, which is called pthreadcondbroadcast or pthreadcondsignal. Mutex can be released and when it happens then other threads that were waiting for their turn can try to lock the Mutex.
The function pthreadcondbroadcast or pthreadcondsignal can be invoked any number of times by any thread in the system even if the thread didn't acquire the mutex lock.
Syntax:
It has the following syntax:
int pthread_cond_broadcast(pthread_cond_t *condition);
Return Value
If the pthreadcondbroadcast and pthreadcondsignal functions are successful, they will return 0; otherwise, the error number will be returned, which indicates the presence of the error.
Application Usage
- The pthreadcondbroadcast function is one of the ways that the shared-variable state can be used for the updation. Through the pthreadcondbroadcast call process, the producer would inform all possible waiting consumers, and as a result, the multi-processor workload distribution would be improved. Also, with the pthreadcondbroadcast, the implementation of a read-write lock is made easier.
- The pthreadcondbroadcast function is used to wake up all the waiting readers when a writer releases the lock. At the end, a two-phase commit operation can rely on the broadcast function to notify all clients about the upcoming transaction commit.
- The pthreadcondsignal function is not safe to use in a signal handler that is invoked by an asynchronous signal process. Because of its complex nature. Hence, the condition variables are not suitable for server signalling.
Example:
//Program to implement the
//pthread_cond_broadcast method in C++
#include <iostream>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t con_var = PTHREAD_COND_INITIALIZER;
bool val = false;
void* threadFun(void* args) {
pthread_mutex_lock(&mutex);
while (!val) {
pthread_cond_wait(&con_var, &mutex);
}
std::cout << "Thread " << *(int*)args << " proceeding.\n";
pthread_mutex_unlock(&mutex);
return nullptr;
}
int main() {
pthread_t thread[2];
int id[2] = {1, 2};
for (int i = 0; i < 2; ++i) {
pthread_create(&thread[i], nullptr, threadFun, &id[i]);
}
sleep(1); // The sleep() function
pthread_mutex_lock(&mutex);
val = true;
pthread_cond_broadcast(&con_var);
pthread_mutex_unlock(&mutex);
for (int i = 0; i < 2; ++i) {
pthread_join(thread[i], nullptr);
}
return 0;
}
Output:
Thread 2 proceeding.
Thread 1 proceeding.
Explanation:
The following is a C++ code that shows the usage of POSIX threads (pthreads) to synchronize the execution of multiple threads by using Condition variables. The code first declares a mutex and a condition variable, the shared boolean variable val. The threadFun function that each thread executes locks the mutex and checks the value of val. If val is false, the thread goes to the wait state about the condition variable and in so doing releases the mutex. The main function starts two threads and both of them execute threadFun; there is a one-second delay between two actions. Following this, the mutex is locked, val turned to true, and pthreadcondbroadcast is used to wake both threads waiting. In the end, one waits for the other thread to complete its execution and then terminates the execution of the program. This program is still helpful and clear in showing how to work with mutexes and condition variables for thread synchronization and communication.