2018-04-04 16:17:13 +02:00
|
|
|
#include <pthread.h>
|
2018-05-06 01:14:09 +02:00
|
|
|
#include <signal.h>
|
2018-04-30 16:15:47 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-05-06 01:14:09 +02:00
|
|
|
#include <time.h> // random seed
|
2018-04-04 16:17:13 +02:00
|
|
|
#include <unistd.h> // sleep
|
|
|
|
|
2018-05-06 18:35:26 +02:00
|
|
|
pthread_mutex_t posConnu;
|
|
|
|
pthread_cond_t newPos;
|
|
|
|
pthread_t tPosition;
|
|
|
|
pthread_t tMovement;
|
2018-04-04 16:17:13 +02:00
|
|
|
|
2018-05-06 18:35:26 +02:00
|
|
|
void* TaskPosition(void* pData)
|
2018-04-30 16:15:47 +02:00
|
|
|
{
|
2018-05-06 18:35:26 +02:00
|
|
|
|
|
|
|
(void)pData;
|
|
|
|
for (;;) {
|
|
|
|
printf("Begin position\n");
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&posConnu);
|
|
|
|
printf("Sending position\n");
|
|
|
|
pthread_cond_signal(&newPos);
|
|
|
|
pthread_mutex_unlock(&posConnu);
|
|
|
|
}
|
|
|
|
return NULL;
|
2018-04-30 16:15:47 +02:00
|
|
|
}
|
2018-04-04 16:17:13 +02:00
|
|
|
|
2018-05-06 18:35:26 +02:00
|
|
|
void* TaskMovement(void* pData)
|
2018-04-04 16:17:13 +02:00
|
|
|
{
|
2018-05-06 18:35:26 +02:00
|
|
|
(void)pData;
|
|
|
|
for (;;) {
|
|
|
|
printf("Begin Movement\n");
|
|
|
|
sleep(3);
|
2018-04-30 16:15:47 +02:00
|
|
|
|
2018-05-06 18:35:26 +02:00
|
|
|
pthread_mutex_lock(&posConnu);
|
|
|
|
printf("Waiting movement\n");
|
|
|
|
pthread_cond_wait(&newPos, &posConnu);
|
|
|
|
pthread_mutex_unlock(&posConnu);
|
2018-05-06 01:14:09 +02:00
|
|
|
|
2018-04-29 09:38:49 +02:00
|
|
|
}
|
2018-05-06 18:35:26 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-04-04 16:17:13 +02:00
|
|
|
|
2018-05-06 18:35:26 +02:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
pthread_mutex_init(&posConnu, NULL);
|
|
|
|
pthread_cond_init(&newPos, NULL);
|
|
|
|
pthread_create(&tPosition, NULL, TaskPosition, NULL);
|
|
|
|
pthread_create(&tMovement, NULL, TaskMovement, NULL);
|
|
|
|
sleep(300);
|
2018-04-04 16:17:13 +02:00
|
|
|
}
|