#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
using namespace std;
#define BOUNDARY 1
int breakcount = 1;
pthread_mutex_t tasks_mutex; //互斥锁
pthread_cond_t tasks_cond; //条件信号变量,处理两个线程间的条件关系,当task>5,hello2处理,反之hello1处理,直到task减为0
int trans;
bool is_signaled = false; //sign
void* say_hello2( void* args )
{
pthread_t pid = pthread_self(); //获取当前线程id
//cout << "[" << pid << "] say_hello2 in thread " << *( ( int* )args ) << endl;
while(1)
{
pthread_mutex_lock( &tasks_mutex ); //加锁
if( is_signaled )
{
cout << "hello 2 ,before,set value of trans="<<trans<< endl;
trans=777;
cout << "hello 2 ,send signal trans="<<trans<< endl;
pthread_cond_signal( &tasks_cond ); //signal:向hello1发送信号,表明已经>5
//is_signaled = true; //表明信号已发送,退出此线程
cout << "hello 2 ,after send signal,value of trans="<<trans<< endl;
breakcount--;
}else{
//is_signaled = false; //表明信号已发送,退出此线程
//cout << "hello 2 ,don't worry,let's watting for hello1 need signal ,trans="<<trans<< endl;
}
pthread_mutex_unlock( &tasks_mutex ); //解锁
//sleep(2);
if( breakcount == 0 )
{
//cout << "hello2 break"<< endl;
break;
}
}
}
void* say_hello1( void* args )
{
pthread_t pid = pthread_self(); //获取当前线程id
//cout << "[" << pid << "] say_hello1 in thread " << *( ( int* )args ) << endl;
while(1)
{
//if( is_signaled ){
cout << "hello1,signed= true"<< endl;
pthread_mutex_lock( &tasks_mutex ); //加锁
cout << "hello1 before get signal, trans="<< trans << endl;
cout << "hello1,waitting signal..."<< endl;
//SendSignal = true;
is_signaled = true; //need signal from hello2
pthread_cond_wait( &tasks_cond, &tasks_mutex ); //wait:等待信号量生效,接收到信号,向hello2发出信号,跳出wait,执行后续
cout << "hello1 after getting signal, trans="<< trans << endl;
pthread_mutex_unlock( &tasks_mutex ); //解锁
//sleep(1);
break;
//}
}
}
int main()
{
pthread_attr_t attr; //线程属性结构体,创建线程时加入的参数
pthread_attr_init( &attr ); //初始化
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ); //是设置你想要指定线程属性参数,这个参数表明这个线程是可以join连接的,join功能表示主程序可以等线程结束后再去做某事,实现了主程序和线程同步功能
pthread_cond_init( &tasks_cond, NULL ); //初始化条件信号量
pthread_mutex_init( &tasks_mutex, NULL ); //初始化互斥量
pthread_t tid1, tid2; //保存两个线程id
int index1 = 101;
int ret = pthread_create( &tid1, &attr, say_hello1, ( void* )&index1 );
if( ret != 0 )
{
cout << "pthread_create error:error_code=" << ret << endl;
}
int index2 = 201;
ret = pthread_create( &tid2, &attr, say_hello2, ( void* )&index2 );
if( ret != 0 )
{
cout << "pthread_create error:error_code=" << ret << endl;
}
pthread_join( tid1, NULL ); //连接两个线程
pthread_join( tid2, NULL );
pthread_attr_destroy( &attr ); //释放内存
pthread_mutex_destroy( &tasks_mutex ); //注销锁
pthread_cond_destroy( &tasks_cond ); //正常退出
}
参考:
https://blog.csdn.net/hitwengqi/article/details/8015646