1. Principle: Understanding the most common use of conditional threads is that two threads handle a queue at the same time, such as a thread responsible for inserting the item when the queue is not full, and another thread responsible for fetching the item when the queue is not empty, the conditional thread involving 4 thread-related functions, pthread_mutex_ Lock, Pthread_mutex_unlock, pthread_cond_wait, pthread_cond_signal, where Pthread_mutex_lock is responsible for line loads locks, To prevent other threads from modifying the shared resource (such as the int cond_num in this example) at the same time, pthread_mutex_unlock the line threads unlocked, pthread_cond_wait is used to block itself from this thread, Pthread_cond_ Signal is used to wake up other threads and must obey the order of Pthread_mutex_lock, pthread_cond_wait, Pthread_cond_signal, Pthread_mutex_unlock.
2. Code examples
#define __use_largefile64
#define _largefile64_source
#ifndef _gnu_source
#define _gnu_source
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
pthread_mutex_t mutex = Pthread_mutex_initializer;
pthread_cond_t Cond_num_even = Pthread_cond_initializer;
pthread_cond_t cond_num_odd = Pthread_cond_initializer;
int cond_num = 1;
void *thread_function_even ()
{
Long unsigned int thread_num = Pthread_self ();
int status = Pthread_detach (Thread_num); /*detach to is an independent thread*/
while (1)
{
printf ("Enter thread_function_even,cond_num:%d\n", cond_num);
Pthread_mutex_lock (&mutex);
while (1 = = cond_num% 2)
Pthread_cond_wait (&cond_num_even, &mutex);
printf ("Enter Thread_function_even,before deal with cond_num,cond_num:%d\n", cond_num);
Cond_num + +; /*cond_num become even,operate it*/
Pthread_cond_signal (&cond_num_odd);/*wake odd function to see if it become odd*/
Pthread_mutex_unlock (&mutex);
}
}
void * Thread_function_odd ()
{
Long unsigned int thread_num = Pthread_self ();
int status = Pthread_detach (Thread_num); /*detach to is an independent thread*/
while (1)
{
printf ("Enter thread_function_odd,cond_num:%d\n", cond_num);
Pthread_mutex_lock (&mutex);
while (0 = = cond_num% 2)
Pthread_cond_wait (&cond_num_odd, &mutex);
printf ("Enter Thread_function_odd,before deal with cond_num,cond_num:%d\n", cond_num);
Cond_num ++;/*cond_num become odd,operate it*/
Pthread_cond_signal (&cond_num_even);/*wake even function to see if it become even*/
Pthread_mutex_unlock (&mutex);
}
}
int main (void)
{
pthread_t thread;
Pthread_create (&thread, NULL, &thread_function_even, NULL);
Pthread_create (&thread, NULL, &thread_function_odd, NULL);
while (1); /*avoid thread ' s quit after main thread quit*/
}
Results:
GCC main.c-lpthread-g
Parsing of the first few rows after/a.out
Enter thread_function_even,cond_num:1/* condition number is odd, direct pthread_cond_wait block jumps out of the function until the number of conditions is even */
Enter thread_function_odd,cond_num:1/* condition number matches odd, skipping blocked lines of code */
Enter Thread_function_odd,before deal with COND_NUM,COND_NUM:1/* handles the number of conditions, the condition number becomes even, and the even thread wakes up because it has not yet been pthread_cond_wait to block itself , continue in the odd while loop */
Enter Thread_function_odd,cond_num:2/* The number of conditions is even, direct pthread_cond_wait block out of the function until the number of conditions is odd, because even has been awakened, skip to even function processing */
Enter Thread_function_even,before deal with COND_NUM,COND_NUM:2/* condition number is even, number of processing conditions */
...
A simple example of conditional threading in multi-threading