This article can be reproduced at will, but the author and source must be indicated.
[Original] Teach you how to design multiple threads in Linux (4)
-- Multi-thread programming in Linux
Original Author: frozen_socker (popsicle)
E_mail: dlskyfly@163.com
Thread Synchronization
First, let's take a look at the concept of synchronization mechanism. Synchronization is the process where several threads wait for an event to occur. when the event occurs, the synchronization starts to run together. We can simply understand synchronization in this way, that is, several threads process their own data, and then the data must be summarized at a certain point. Otherwise, the next step cannot be processed. Function calls for thread synchronization include pthread_cond_init, pthread_cond_broadcast, pthread_cond_signal, pthread_cond_wait, and pthread_cond_destroy.
Function prototype: Int pthread_cond_init (pthread_cond_t * restrict cond,
Const pthread_condattr_t * restrict ATTR );
Function Description:
Initialize the cond condition Variable Based on the attribute specified by ATTR. If ATTR is null, the effect is equivalent to pthread_cond_t cond = pthread_cond_initializer.
Function prototype:
Int pthread_cond_broadcast (pthread_cond_t * Cond );
Function Description:
All threads waiting for the cond condition variable are blocked.
Function prototype:
Int pthread_cond_signal (pthread_cond_t * Cond );
Function Description:
Only remove the blocking status of a thread waiting for the condition variable cond. If several threads are suspended and waiting for this condition variable, the call only invokes one thread. Which of the following threads is unknown.
Function prototype:
Int pthread_cond_wait (pthread_cond_t * restrict cond,
Pthread_mutex_t * restrict mutex );
Function Description:
The call automatically blocks the current thread that sends the call, waits for the condition variable specified by the cond parameter, and unlocks the mutex specified by the mutex parameter. The blocked thread is not blocked until other threads call the pthread_cond_signal or pthread_cond_broadcast function to set the corresponding conditional variable and obtain the mutex. Threads in the waiting state do not occupy CPU time.
Function prototype:
Int pthread_cond_timedwait (pthread_cond_t * restrict cond,
Pthread_mutex_t * restrict mutex,
Const struct timespec * restrict abstime );
Function Description:
This function automatically blocks the condition variable specified by the current thread waiting for the cond parameter and unlocks the mutex specified by the mutex parameter. The blocked thread is invoked to continue execution. Other threads call the pthread_cond_signal function for the condition variable cond, or other threads call pthread_cond_broadcast for the condition variable cond; or the time when the system time reaches the time specified by the abstime parameter. In addition to one of the first three conditions, the thread must also obtain the mutex specified by the mutex parameter.
Function prototype:
Int pthread_cond_destroy (pthread_cond_t * Cond );
Function Description:
Releases the resources occupied by the cond condition variable.
See the following example:
// Example_5.c
# Include <stdio. h>
# Include <pthread. h>
Pthread_t pt1, pt2;
Pthread_mutex_t mu;
Pthread_cond_t cond;
Int I = 1;
Void * decrease (void * Arg)
...{
While (1)
...{
Pthread_mutex_lock (& mu );
If (++ I)
...{
Printf ("% d", I );
If (I! = 1) printf ("error ");
Pthread_cond_broadcast (& Cond );
Pthread_cond_wait (& cond, & mu );
}
Sleep (1 );
Pthread_mutex_unlock (& mu );
}
}
Void * increase (void * Arg)
...{
While (1)
...{
Pthread_mutex_lock (& mu );
If (I --)
...{
Printf ("% d", I );
If (I! = 0) printf ("error ");
Pthread_cond_broadcast (& Cond );
Pthread_cond_wait (& cond, & mu );
}
Sleep (1 );
Pthread_mutex_unlock (& mu );
}
}
Int main ()
...{
Pthread_create (& pt2, null, increase, null );
Pthread_create (& pt1, null, decrease, null );
Pthread_join (pt1, null );
Pthread_join (pt2, null );
}
We have explained the multi-thread programming knowledge of using the pthread. h header file in Linux. In the next chapter, we will continue to explain the deep programming knowledge about multithreading.