linux 多线程编程 互斥锁与条件变量

条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待 “ 条件变量的条件成立” 而挂起,另一个线程使 “条件成立 ”(给出条件成立信号),为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。

下面这个例子展示的是互斥锁和条件变量的结合使用,以及取消对于条件等待动作的影响,例子中有两个线程被启动,并等待同一个条件变量。

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

pthread_mutex_t mutex; // 互斥量
pthread_cond_t  cond; // 条件变量

void* child1( void* param )
{
        pthread_cleanup_push( (void (*)(void*))pthread_mutex_unlock, (void*)&mutex );
        while( 1 )
        {
                printf( "thread 1 get running\n" );
                printf( "thread 1 pthread_mutex_lock returns %d\n", pthread_mutex_lock( &mutex ) );
                pthread_cond_wait( &cond, &mutex ); // 无条件等待
                printf( "thread 1 condition applied\n" );
                pthread_mutex_unlock( &mutex );

                sleep( 5 );
        }
        pthread_cleanup_pop( 0 );

        return ( void* )0;
}

void* child2( void* param )
{
        while( 1 )
        {
                sleep( 3 );
                printf( "thread 2 get running\n" );
                printf( "thread 2 pthread_mutex_lock returns %d\n", pthread_mutex_lock( &mutex ) );
                pthread_cond_wait( &cond, &mutex ); // 无条件等待
                printf( "thread 2 condition applied\n" );
                pthread_mutex_unlock( &mutex );

                sleep( 1 );
        }

        return ( void* )0;
}

int main()
{
        pthread_t tid1, tid2;
        printf( "hello, condition variable test\n" );

        pthread_mutex_init( &mutex, NULL ); // 初始化 互斥锁
        pthread_cond_init( &cond, NULL ); // 初始化 条件变量

        pthread_create( &tid1, NULL, child1, NULL ); // new 线程 tid1
        pthread_create( &tid2, NULL, child2, NULL ); // new 线程 tid2

        do
        {
                sleep( 2 );
                pthread_cancel( tid1 ); // 发送终止信号给 tid1,具体请google 取消点 cancel point
                sleep( 2 );
                pthread_cond_signal( &cond ); // 激活一个等待该条件的线程
        }while( 1 );

        sleep( 100 );
        pthread_exit( 0 );

        return 0;
}

 

// pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mutex,然后阻塞在等待队列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mutex);,再读取资源


// 编译

kennie@cbib:~/pthreadDIR$ g++ -lpthread -o mutex_con.out mutex_con.cpp

// 运行结果

hello, condition variable test
thread 1 get running
thread 1 pthread_mutex_lock returns 0
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
......


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值