Pembolehubah global dimulakan kepada 0. Dua utas, satu utas menambahnya sebanyak 1 setiap kali Apabila lebih besar daripada atau sama dengan 100, hantarkan syarat kepada utas yang satu lagi
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct foo{
int f_count;
pthread_mutex_t f_lock;
pthread_cond_t f_cond;
};
/* 初始化互斥量與條件變量 */
struct foo * foo_alloc(){
struct foo *fp;
if((fp = malloc(sizeof(struct foo))) != NULL){
fp->f_count = 0;
pthread_mutex_init(&fp->f_lock, NULL);
pthread_cond_init(&fp->f_cond, NULL);
}
return fp;
}
/* 加法 */
void *foo_increase(void *arg){
struct foo *fp;
fp = (struct foo*)arg;
while(1){
pthread_mutex_lock(&fp->f_lock);
fp->f_count++;
/* 大于等于100時(shí)發(fā)送條件 */
if(fp->f_count >= 100){
pthread_cond_signal(&fp->f_cond);
}
pthread_mutex_unlock(&fp->f_lock);
}
}
/* 重新置0 */
void *foo_print(void *arg){
struct foo *fp;
fp = (struct foo*)arg;
while(1){
pthread_mutex_lock(&fp->f_lock);
while(fp->f_count < 100){
//釋放掉鎖, 等待條件為真返回, 再次鎖住.
pthread_cond_wait(&fp->f_cond, &fp->f_lock);
}
printf("重置 : %d\n", fp->f_count);
/* 重新置0 */
fp->f_count = 0;
pthread_mutex_unlock(&fp->f_lock);
}
}
int main(void){
struct foo *fp;
pthread_t tid_increase1, tid_print;
//初始化
fp = foo_alloc();
//加法線程
pthread_create(&tid_increase1, NULL, foo_increase, fp);
//重置線程
pthread_create(&tid_print, NULL, foo_print, fp);
//防止主線程提前退出
sleep(20);
exit(0);
}
Tetapi mengapa hasil berikut dicetak apabila saya mencetaknya di bawah benang tetapan semula, bukankah sepatutnya 100?Benang tetapan semula telah mengunci mutex, tetapi nampaknya benang lain masih terkumpul.
置0前 : 54
置0前 : 92
置0前 : 47
置0前 : 85
置0前 : 51
...
閉關(guān)修行中......
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
struct foo{
int f_count;
pthread_mutex_t f_lock;
pthread_cond_t f_cond;
};
/* 初始化互斥量與條件變量 */
struct foo * foo_alloc(){
struct foo *fp;
if((fp = (struct foo *)malloc(sizeof(struct foo))) != NULL){
fp->f_count = 0;
pthread_mutex_init(&fp->f_lock, NULL);
pthread_cond_init(&fp->f_cond, NULL);
}
return fp;
}
/* 加法 */
void *foo_increase(void *arg){
struct foo *fp;
fp = (struct foo*)arg;
while(1){
pthread_mutex_lock(&fp->f_lock);
fp->f_count++;
if(fp->f_count >= 100){
pthread_cond_signal(&fp->f_cond);
//-------------------這里要等待,不然會(huì)再次循環(huán)+1
pthread_cond_wait(&fp->f_cond, &fp->f_lock);
}
pthread_mutex_unlock(&fp->f_lock);
/* 大于等于100時(shí)發(fā)送條件 */
}
}
/* 重新置0 */
void *foo_print(void *arg){
struct foo *fp;
fp = (struct foo*)arg;
while(1){
pthread_mutex_lock(&fp->f_lock);
while(fp->f_count < 100){
//釋放掉鎖, 等待條件為真返回, 再次鎖住.
pthread_cond_wait(&fp->f_cond, &fp->f_lock);
}
printf("重置 : %d\n", fp->f_count);
/* 重新置0 */
fp->f_count = 0;
//-------------------加這條就不會(huì)阻塞了foo_increase里的wait
pthread_cond_signal(&fp->f_cond);
pthread_mutex_unlock(&fp->f_lock);
}
}
int main(void){
struct foo *fp;
pthread_t tid_increase1, tid_print;
//初始化
fp = foo_alloc();
//加法線程
pthread_create(&tid_increase1, NULL, foo_increase, fp);
//重置線程
pthread_create(&tid_print, NULL, foo_print, fp);
//防止主線程提前退出
sleep(2);這里這時(shí)要
free(fp);//DO NOT FORGET
exit(0);