多线程中的条件线程简单示例

来源:互联网 发布:linux使用gcc编译器 编辑:程序博客网 时间:2024/06/05 03:14

1.原理:理解的条件线程最常见的用法就是两个线程同时对一个队列进行处理,比如一个线程负责在队列未满的时候插入item,另外一个线程负责在队列非空时取出item,条件线程涉及4个线程相关函数,pthread_mutex_lock、pthread_mutex_unlock、pthread_cond_wait、pthread_cond_signal,其中pthread_mutex_lock负责对线程加锁,以免其他线程同时对共享资源(比如本例中的int cond_num)进行修改,pthread_mutex_unlock对线程解锁,pthread_cond_wait用于阻塞自己这个线程,pthread_cond_signal用于唤醒其他线程,必须遵守pthread_mutex_lock、pthread_cond_wait、pthread_cond_signal、pthread_mutex_unlock的顺序。


2.代码示例

#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 be 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 be 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*/
}


结果:
gcc main.c -lpthread -g
./a.out 后的前几行结果解析
enter thread_function_even,cond_num:1 /*条件数为奇数,直接pthread_cond_wait阻塞跳出该函数直至条件数为偶数为止*/
enter thread_function_odd,cond_num:1 /*条件数符合奇数,跳过阻塞的代码行*/
enter thread_function_odd,before deal with cond_num,cond_num:1 /*处理条件数,条件数变为偶数,将even线程唤醒,因为还未发pthread_cond_wait阻塞自己,继续在odd的while循环中进行*/
enter thread_function_odd,cond_num:2 /*条件数为偶数,直接pthread_cond_wait阻塞跳出该函数直至条件数为奇数为止,因为even已唤醒,跳到even函数处理*/
enter thread_function_even,before deal with cond_num,cond_num:2 /*条件数为偶数,处理条件数*/
...

0 0
原创粉丝点击