我正在学习C语言中的实时编程,但我仍然是一个初学者。 我创建了一个简单的定期任务来计算总和,现在我试图同步两个实时任务(定期任务),其中一个任务将使用另一个任务产生的结果。
我的问题是如何同步定期任务?现在,我想这样做,然后尝试使用优先级。
我的第二个问题是如何使用pthread_create(&th2, NULL, tache2, "2")
返回int ??
非常感谢您的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
sem_t evt;
void* tache1(void *arg){
int i=0;
while(i<2){
printf("La tache %s s'execute\n", (char*) arg);
sem_post(&evt);
i++;
}
}
void* tache2(void *arg){
int i=0;
while(i<2){
sem_wait(&evt);
printf("La tache %s s'execute enfin\n", (char*) arg);
i++;
}
}
int main()
{
pthread_t th1, th2;
sem_init(&evt, 0,2 );
pthread_create(&th1, NULL, tache1, "1");
pthread_create(&th2, NULL, tache2, "2");
pthread_join(th1, NULL);
pthread_join(th2, NULL);
return 0;
}