C Var条件按特定顺序执行线程

C Var条件按特定顺序执行线程,c,multithreading,pthreads,C,Multithreading,Pthreads,如何按特定顺序显示线程? 我需要按照以下执行顺序打印条件变量,但我不知道如何执行 我需要打印订单吗 一, 3. 1. 2. 一, #包括 #包括 #包括 结构DatoHilo{int dato;}; void*funhilos(void*); int-turno=0; pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项; pthread_cond_t cond=pthread_cond_初始值设定项; int main(){ 时钟,时间,时间; 双

如何按特定顺序显示线程? 我需要按照以下执行顺序打印条件变量,但我不知道如何执行

我需要打印订单吗

一, 3. 1. 2. 一,

#包括
#包括
#包括
结构DatoHilo{int dato;};
void*funhilos(void*);
int-turno=0;
pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项;
pthread_cond_t cond=pthread_cond_初始值设定项;
int main(){
时钟,时间,时间;
双秒;
int-nhilos=0,i=0;
pthread_t*pidhilos=NULL;
结构DatoHilo*DatoHilo;
printf(“hilos的数字”);
scanf(“%d”&nhilos);
//希洛斯酒店
pidhilos=(pthread_t*)calloc(nhilos,sizeof(pthread_t));
对于(i=0;idato=i;
pthread_create(&pidhilos[i],NULL,funhilos,(void*)datohilo);
}
t_ini=时钟();
对于(i=0;idato;
pthread_mutex_lock(&mutex);
while(turno!=myturno)pthread_cond_wait(&cond,&mutex);
printf(“Hilo turno%d\t[%u]\n”,myturno,(unsigned int)pthread_self());
turno++;
pthread_cond_广播(&cond);
pthread_mutex_unlock(&mutex);
自由(arg);
pthread_退出(0);
}

如果您的订单是
1 3 1 2 1 3…
并且您确实想要使用线程,那么您需要三个条件变量-每个线程一个,每次我们都会向下一个需要的线程发送信号

这是线程
N
的基本代码:

pthread_mutex_lock(m);
while(!awakenedN)
    pthread_cond_wait(condN, m);
printf("thread N"); //Nth thread's work
pthread_cond_signal(...); //awaken next thread
awakened... = 1;
awakenedN = 0;
pthread_mutex_unlock(m);
因此,您只需选择
代表什么,选择是显而易见的:

int awakened1 = 1, awakened2 = 0, awakened3 = 0;
int turn = 1;

// thread 1
pthread_mutex_lock(m);
while(!awakened1)
    pthread_cond_wait(cond1, m);
printf("thread 1");
// after first thread we can call second or third:
switch(turn) {
    case 1:
        turn = 2;
        awakened3 = 1;
        pthread_cond_signal(cond3);
        break;
    case 2:
        turn = 1;
        awakened2 = 1;
        pthread_cond_signal(cond2);
        break;
}
awakened1 = 0;
pthread_mutex_unlock(m);

// thread 2
pthread_mutex_lock(m);
while(!awakened2)
    pthread_cond_wait(cond2, m);
printf("thread 2");
awakened1 = 1;
awakened2 = 0;
pthread_cond_signal(cond1);
pthread_mutex_unlock(m);

// thread 3
pthread_mutex_lock(m);
while(!awakened3)
    pthread_cond_wait(cond3, m);
printf("thread 3");
awakened1 = 1;
awakened3 = 0;
pthread_cond_signal(cond1);
pthread_mutex_unlock(m);

在这之后,对于大于3的
N
(线程数)可以很容易地将其推广。

格式化代码以便于阅读
int awakened1 = 1, awakened2 = 0, awakened3 = 0;
int turn = 1;

// thread 1
pthread_mutex_lock(m);
while(!awakened1)
    pthread_cond_wait(cond1, m);
printf("thread 1");
// after first thread we can call second or third:
switch(turn) {
    case 1:
        turn = 2;
        awakened3 = 1;
        pthread_cond_signal(cond3);
        break;
    case 2:
        turn = 1;
        awakened2 = 1;
        pthread_cond_signal(cond2);
        break;
}
awakened1 = 0;
pthread_mutex_unlock(m);

// thread 2
pthread_mutex_lock(m);
while(!awakened2)
    pthread_cond_wait(cond2, m);
printf("thread 2");
awakened1 = 1;
awakened2 = 0;
pthread_cond_signal(cond1);
pthread_mutex_unlock(m);

// thread 3
pthread_mutex_lock(m);
while(!awakened3)
    pthread_cond_wait(cond3, m);
printf("thread 3");
awakened1 = 1;
awakened3 = 0;
pthread_cond_signal(cond1);
pthread_mutex_unlock(m);