多线程处理信号量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sharon_1987/article/details/51396071

include

include

include

define BOUNDARY 5

int tasks = 10;
pthread_mutex_t tasks_mutex;
pthread_cond_t tasks_cond;

static void *say_hello2(void *args)
{
pthread_t pid = pthread_self();
cout << “[” << pid << “] hello in thread ” << ((int)args) << endl;

bool is_signaled = false;
while (1) {
pthread_mutex_lock(&tasks_mutex);
if (tasks > BOUNDARY) {
cout << “[” << pid << “] take task : ” << tasks << ” in thread ” << ((int)args) << endl;
– tasks;
} else if (!is_signaled) {
cout << “[” << pid << “] pthread_cond_signal in thread ” << ((int)args) << endl;
pthread_cond_signal(&tasks_cond);
is_signaled = true;
}
pthread_mutex_unlock(&tasks_mutex);
if (tasks == 0) {
break;
}
}
}
static void *say_hello1(void *args)
{
pthread_t pid = pthread_self();
cout << “[” << pid << “] hello in thread ” << ((int)args) << endl;

bool is_signaled = false;
while (1) {
pthread_mutex_lock(&tasks_mutex);
if (tasks > BOUNDARY) {
cout << “[” << pid << “] pthread_cond_signal in thread ” << ((int)args) << endl;
pthread_cond_wait(&tasks_cond, &tasks_mutex);
} else {
cout << “[” << pid << “] take task : ” << tasks << ” in thread ” << ((int)args) << endl;
– tasks;
}
pthread_mutex_unlock(&tasks_mutex);
if (tasks == 0) {
break;
}
}
}

int main()
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

pthread_mutex_init(&tasks_mutex, NULL);
pthread_cond_init(&tasks_cond, NULL);

pthread_t tid1, tid2;
int index1 = 1;
int ret = pthread_create(&tid1, &attr, say_hello1, (void*)&(index1));
if (ret != 0) {
cout << “pthread_create error:error_code=” << ret << endl;
}

int index2 = 12;
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);
}

运行方式:
g++ -o muti_thread_test_2 muti_thread_test_2.cpp -lpthread
./muti_thread_test_2

结果:
[[139647987828480139647977338624] hello in thread ] hello in thread 121
[139647977338624] take task :
10 in thread 12
[139647977338624] take task : 9 in thread 12
[139647987828480] pthread_cond_signal in thread 1
[139647977338624] take task : 8 in thread 12
[139647977338624] take task : 7 in thread 12
[139647977338624] take task : 6 in thread 12
[139647977338624] pthread_cond_signal in thread 12
[139647987828480] take task : 5 in thread 1
[139647987828480] take task : 4 in thread 1
[139647987828480] take task : 3 in thread 1
[139647987828480] take task : 2 in thread 1
[139647987828480] take task : 1 in thread 1

again:
[[140119321384704140119310894848] hello in thread ] hello in thread 112
[140119310894848
] take task : 10 in thread 12
[140119310894848] take task : 9 in thread 12
[140119321384704] pthread_cond_signal in thread 1
[140119310894848] take task : 8 in thread 12
[140119310894848] take task : 7 in thread 12
[140119310894848] take task : 6 in thread 12
[140119310894848] pthread_cond_signal in thread 12
[140119321384704] take task : 5 in thread 1
[140119321384704] take task : 4 in thread 1
[140119321384704] take task : 3 in thread 1
[140119321384704] take task : 2 in thread 1
[140119321384704] take task : 1 in thread 1

猜你喜欢

转载自blog.csdn.net/sharon_1987/article/details/51396071