我正在用pthreads做一个项目,我不能走得更远。我有3个线程,一个线程应该通过I2C(SMBUS)读取一些传感器值,一个应该使用第一个线程占用的数据计算一些东西,最后一个线程应该在屏幕上打印数据。互斥体应负责线程之间的同步。我如何在线程中使用无限循环,因为我的线程不只是“完成任务并完成”?他们应该永远持续下去(或者直到按下Ctrl + C键)。pthread中可能有无限循环吗?
我应该在哪里放置pthread_join
函数?
我的第一种方法是在下面,但它不能正常工作:
感谢您的帮助。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
typedef struct vector {
double x;
double y;
double z;
} vector_t;
typedef struct all_vectors {
vector_t *gyro;
vector_t *accel;
vector_t *magnet;
} vectors_t;
vectors_t *vectors;
pthread_t th1, th2, th3;
pthread_mutex_t mutex;
int main()
{
initSensors(); //defined in another .c file
pthread_mutex_init(&mutex, NULL);
pthread_create(&th1, NULL, (void *) &readSensors, (void *) vectors);
pthread_create(&th2, NULL, (void *) &calculateAngle, NULL);
pthread_create(&th3, NULL, (void *) &printData, NULL);
while(1)
{
sleep(1);
}
/* The program never reaches the following codes ? */
pthread_mutex_destroy(&mutex);
pthread_exit(NULL);
}
void readSensors(void *vectors)
{
vectors_t *vecs = (vectors_t *)vectors;
while(1)
{
pthread_mutex_trylock(&mutex);
readGyro(vecs->gyro); //defined in another .c file
readAccel(vecs->accel); //defined in another .c file
readMagnet(vecs->magnet); //defined in another .c file
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void calculateAngle()
{
while(1)
{
pthread_mutex_trylock(&mutex);
doSomeCalculation(vectors); //defined in another .c file
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void printData()
{
while(1)
{
pthread_mutex_trylock(&mutex);
printf("%lf, %lf, %lf, lf, %lf, %lf, lf, %lf, %lf", \
vectors->accel->x, vectors->accel->y, vectors->accel->z, \
vectors->gyro->x, vectors->gyro->y, vectors->gyro->z, \
vectors->magnet->x, vectors->magnet->y, vectors->magnet->z);
pthread_mutex_unlock(&mutex);
fflush(stdout);
}
pthread_exit(NULL);
}
你有4个无限循环 - 什么不工作?如果你打算让你的线程永久运行,那么加入它们就没有意义了 - 将它们创建为分离的。你真的似乎缺少的是沟通和协调线程努力的一种方式。 – Duck
如果我不使用线程,数据显示正确,但如果我使用线程数据显示不正确。 –
我知道。你现在有4个独立的演员。你必须协调他们。有1个线程读取,1个线程计算和1个线程打印是一回事。让他们以正确的顺序完成所有工作以产生预期产出是另一回事。你需要查看队列,条件变量等内容,如果你想这个工作。 – Duck