編寫一個程式,開啟3個執行緒,這3個執行緒的ID分別為A、B、C,每個執行緒將自己的ID在螢幕上列印10遍
阿新 • • 發佈:2018-12-31
#include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <string.h>
- //#define DEBUG 1
- #define NUM 3
- int n=0;
- pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;//互斥量
- pthread_cond_t qready=PTHREAD_COND_INITIALIZER;//條件變數
- void * thread_func(void *arg)
- {
- int param=(int)arg;
- char c='A'+param;
- int ret,i=0;
- for (; i < 10; i++)
- {
- pthread_mutex_lock(&mylock);
- while (param != n) //剛執行時,n = 0, param = 0,條件不成立,所以直接列印A
- {
- #ifdef DEBUG
- printf("thread %d waiting\n", param);
- #endif
- ret = pthread_cond_wait(&qready, &mylock);
- if (ret == 0)
- {
- #ifdef DEBUG
- printf("thread %d wait success\n", param);
- #endif
- } else
- {
- #ifdef DEBUG
- printf("thread %d wait failed:%s\n", param, strerror(ret));
- #endif
- }
- }
- // printf("%d ",param+1);
- printf(
- n=(n+1)%NUM; //n變成了1,對執行緒2會產出影響!!!!
- pthread_mutex_unlock(&mylock);
- //會喚醒所有的執行緒,因為當這個執行緒完後會等pthread_cond_wait()執行兩次後才能退出while (param != n)
- pthread_cond_broadcast(&qready);
- }
- return (void *)0;
- }
- #if 0
- //假設為執行緒2
- void * thread_func(void *arg)//傳入值1
- {
- int param=(int)arg;
- char c='A'+param;
- int ret,i=0;
- for (; i < 10; i++)
- {
- pthread_mutex_lock(&mylock);
- while (param != n) //和執行緒1同時執行,所以剛開始時條件滿足
- {
- #ifdef DEBUG
- printf("thread %d waiting\n", param);
- #endif
- //執行到此時,等待執行緒1傳送訊號,當執行緒1的A列印完後,n的值也變成了1,條件就不成立了
- ret = pthread_cond_wait(&qready, &mylock);
- if (ret == 0)
- {
- #ifdef DEBUG
- printf("thread %d wait success\n", param);
- #endif
- } else
- {
- #ifdef DEBUG
- printf("thread %d wait failed:%s\n", param, strerror(ret));
- #endif
- }
- }
- // printf("%d ",param+1);
- printf("%c ",c); //此時列印值B
- n=(n+1)%NUM; //對列印C的執行緒3產生影響!!!
- pthread_mutex_unlock(&mylock);
- pthread_cond_broadcast(&qready);
- }
- return (void *)0;
- }
- #endif
- int main(int argc, char** argv) {
- int i=0,err;
- pthread_t tid[NUM];
- void *tret;
- for(;i<NUM;i++)
- {
- err=pthread_create(&tid[i],NULL,thread_func,(void *)i);
- if(err!=0)
- {
- printf("thread_create error:%s\n",strerror(err));
- exit(-1);
- }
- }
- for (i = 0; i < NUM; i++)
- {
- err = pthread_join(tid[i], &tret);
- if (err != 0)
- {
- printf("can not join with thread %d:%s\n", i,strerror(err));
- exit(-1);
- }
- }
- printf("\n");
- return 0;
- }
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
//#define DEBUG 1
#define NUM 3
int n=0;
pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;//互斥量
pthread_cond_t qready=PTHREAD_COND_INITIALIZER;//條件變數
void * thread_func(void *arg)
{
int param=(int)arg;
char c='A'+param;
int ret,i=0;
for (; i < 10; i++)
{
pthread_mutex_lock(&mylock);
while (param != n) //剛執行時,n = 0, param = 0,條件不成立,所以直接列印A
{
#ifdef DEBUG
printf("thread %d waiting\n", param);
#endif
ret = pthread_cond_wait(&qready, &mylock);
if (ret == 0)
{
#ifdef DEBUG
printf("thread %d wait success\n", param);
#endif
} else
{
#ifdef DEBUG
printf("thread %d wait failed:%s\n", param, strerror(ret));
#endif
}
}
// printf("%d ",param+1);
printf("%c ",c); //列印A後
n=(n+1)%NUM; //n變成了1,對執行緒2會產出影響!!!!
pthread_mutex_unlock(&mylock);
//會喚醒所有的執行緒,因為當這個執行緒完後會等pthread_cond_wait()執行兩次後才能退出while (param != n)
pthread_cond_broadcast(&qready);
}
return (void *)0;
}
#if 0
//假設為執行緒2
void * thread_func(void *arg)//傳入值1
{
int param=(int)arg;
char c='A'+param;
int ret,i=0;
for (; i < 10; i++)
{
pthread_mutex_lock(&mylock);
while (param != n) //和執行緒1同時執行,所以剛開始時條件滿足
{
#ifdef DEBUG
printf("thread %d waiting\n", param);
#endif
//執行到此時,等待執行緒1傳送訊號,當執行緒1的A列印完後,n的值也變成了1,條件就不成立了
ret = pthread_cond_wait(&qready, &mylock);
if (ret == 0)
{
#ifdef DEBUG
printf("thread %d wait success\n", param);
#endif
} else
{
#ifdef DEBUG
printf("thread %d wait failed:%s\n", param, strerror(ret));
#endif
}
}
// printf("%d ",param+1);
printf("%c ",c); //此時列印值B
n=(n+1)%NUM; //對列印C的執行緒3產生影響!!!
pthread_mutex_unlock(&mylock);
pthread_cond_broadcast(&qready);
}
return (void *)0;
}
#endif
int main(int argc, char** argv) {
int i=0,err;
pthread_t tid[NUM];
void *tret;
for(;i<NUM;i++)
{
err=pthread_create(&tid[i],NULL,thread_func,(void *)i);
if(err!=0)
{
printf("thread_create error:%s\n",strerror(err));
exit(-1);
}
}
for (i = 0; i < NUM; i++)
{
err = pthread_join(tid[i], &tret);
if (err != 0)
{
printf("can not join with thread %d:%s\n", i,strerror(err));
exit(-1);
}
}
printf("\n");
return 0;
}
結果如下: