您好,登錄后才能下訂單哦!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/time.h>
- #include <stdbool.h>
- #include <errno.h>
- #include <unistd.h>
- # define satisfy true
- # define unsatisfy false
- //pthread_mutex_t mut_loc = PTHREAD_MUTEX_INITIALIZER;//所為靜態初始化為PTHREAD_MUTEX_INITIALIZER為一個常量,在全局中進行賦值
- pthread_mutex_t mut_loc;
- pthread_cond_t thd_con;
- struct timespec tsp;
- bool condition = unsatisfy;
- void maketimeout(struct timespec *tsp, int add_sec)//設置超時函數,當前的時間再加上需要等待時候,因為pthread_cond_timedwait只認識當前時間格式
- {
- struct timeval now;
- gettimeofday(&now,NULL);//獲取當前時間
- tsp->tv_sec = now.tv_sec;
- tsp->tv_nsec = now.tv_usec *1000;
- tsp->tv_sec += add_sec; //等待20秒
- }
- void thread0(void)
- {
- int ret;
- if(0 != pthread_mutex_lock(&mut_loc))//上鎖
- {
- perror("pthread_mutex_lock_0\n");
- return;
- }
- if(condition == unsatisfy)
- {
- fputs("條件不滿足,重新等待條件!!\n",stdout);
- maketimeout(&tsp, 2);//設置超時時間2秒
- //等待條件,并且解鎖,線程轉到等待隊列中,如果條件滿足信號收到,即會進行上鎖;
- //線程是處于一種叫做無競爭方式,由于條件未滿足情況下不會與其它線程競爭鎖,只有條件滿足后才會進行競爭
- ret = pthread_cond_timedwait(&thd_con,&mut_loc,&tsp);
- if(ETIMEDOUT == ret)
- {
- fputs("直到超時條件都不滿足,重新等待條件!!\n",stdout);
- }
- else if(0 == ret)
- {
- fputs("線程0在等待時候獲得條件滿足!\n",stdout);
- }
- else
- {
- perror("other error for pthread_cond_timedwait \n");
- pthread_exit((void *)1);
- }
- if(condition == satisfy)
- {
- fputs("0_條件滿足!!\n",stdout);
- condition = unsatisfy;
- }
- }
- else if(condition == satisfy)
- {
- fputs("1_條件滿足!!\n",stdout);
- condition = unsatisfy;
- }
- else
- {
- perror("error condition\n ");
- pthread_exit((void *)1);
- }
- if(0 != pthread_mutex_unlock(&mut_loc))
- {
- perror("pthread_mutex_lock_0\n");
- return;
- }
- pthread_exit((void *)0);
- }
- void thread1(void)
- {
- int ret;
- ret = pthread_mutex_trylock(&mut_loc);
- if(EBUSY == ret)
- {
- fputs("鎖被線程0所占有!\n",stdout);
- }
- else if(0 == ret)
- {
- if(0 != pthread_cond_signal(&thd_con))
- {
- perror("pthread_cond_signal\n");
- pthread_exit((void *)1);
- }
- condition = satisfy;
- fputs("線程1使條件滿足\n",stdout);
- if(0 != pthread_mutex_unlock(&mut_loc))
- {
- perror("pthread_mutex_lock_1\n");
- pthread_exit((void *)1);
- }
- }
- else
- {
- perror("other errors for pthread_mutex_lock_1\n");
- pthread_exit((void *)1);
- }
- pthread_exit((void *)0);
- }
- int main(int argc, char* argv[])
- {
- pthread_t thd0, thd1;
- if(0 != pthread_mutex_init(&mut_loc,NULL))// pthread_mutex_init 與 pthread_mutex_destroy配對使用,因為其是動態即使用malloc來產生
- {
- perror("pthread_mutex_init\n");
- exit(1);
- }
- if(0 != pthread_cond_init(&thd_con,NULL))// pthread_cond_init 與 pthread_cond_destroy配對使用,因為其是動態即使用malloc來產生
- {
- perror("pthread_cond_init\n");
- exit(1);
- }
- if(0 != pthread_create(&thd0,NULL,(void*)thread0,NULL))//創建線程0
- {
- perror("pthread_create_0\n");
- exit(1);
- }
- sleep(1);//讓線程0先執行
- if(0 != pthread_create(&thd1,NULL,(void*)thread1,NULL))//創建線程1
- {
- perror("pthread_create_0\n");
- exit(1);
- }
- if(0 != pthread_join(thd1,NULL))//如果線程牌分離屬性此函數不可用,如果線程1不退出,則處于阻塞狀態
- {
- perror("pthread_join_0\n");
- exit(1);
- }
- if(0 != pthread_join(thd0,NULL))//如果線程牌分離屬性此函數不可用,如果線程0不退出,則處于阻塞狀態
- {
- perror("pthread_join_1\n");
- exit(1);
- }
- if(0 != pthread_cond_destroy(&thd_con))//與pthread_cond_init配對使用
- {
- perror("pthread_cond_destory\n");
- exit(1);
- }
- if(0 != pthread_mutex_destroy(&mut_loc))//與pthread_mutex_init配對使用
- {
- perror("pthread_mutex_init\n");
- exit(1);
- }
- return 0;
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。