如何在Linux C上同步两个线程?
所以我想通过这段代码同步两个线程。顺便说一下,让我解释一下,我想让thread1从传感器读取一个值,然后将其写入一个名为“integers.dat”的文件。因此,thread2的任务是将先前写在“integers.dat”上的内容发送到gnuplot(如下代码所示)。因此,我希望线程执行顺序如下所示: 线程1(写入)、线程2(发送)、线程1(写入)、线程2(发送)等等。 我试图用互斥来实现这一点,但没有成功。两个线程的执行总是随机的 代码: `如何在Linux C上同步两个线程?,c,linux,raspberry-pi3,C,Linux,Raspberry Pi3,所以我想通过这段代码同步两个线程。顺便说一下,让我解释一下,我想让thread1从传感器读取一个值,然后将其写入一个名为“integers.dat”的文件。因此,thread2的任务是将先前写在“integers.dat”上的内容发送到gnuplot(如下代码所示)。因此,我希望线程执行顺序如下所示: 线程1(写入)、线程2(发送)、线程1(写入)、线程2(发送)等等。 我试图用互斥来实现这一点,但没有成功。两个线程的执行总是随机的 代码: ` #include <stdio.h
#include <stdio.h>
#include <pigpio.h>
#include<sys/types.h>
#include<signal.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#define TRIGGER 5
#define ECHO 6
void *thread1_process (void *arg);
void *thread2_process( void *arg);
double start, stop, measure;
int i, val, it=0;
FILE *fptr;
FILE *gnu ;
static pthread_mutex_t my_mutex11;
int main(int argc, char *argv[]) {
pthread_t th1, th2;
void *ret;
fptr=fopen("integers.dat", "w");
gnu = popen("gnuplot -persistent","w");
pthread_mutex_init (&my_mutex11, NULL);
pthread_create(&th1, NULL, thread1_process, NULL);
pthread_create (&th2, NULL, thread2_process, NULL);
(void)pthread_join (th1, &ret);
(void)pthread_join (th2, &ret);
}
void *thread1_process (void *arg)
{
for(int i=0; i<20; i++)
{
pthread_mutex_lock (&my_mutex11);
printf("thread1 %d \n", i);
gpioInitialise();
gpioSetMode(TRIGGER , PI_OUTPUT); // trigger
gpioSetMode(ECHO , PI_INPUT);
gpioWrite(TRIGGER, 0);
gpioSleep(PI_TIME_RELATIVE, 0, 1);
gpioWrite(TRIGGER, 1);
gpioSleep(PI_TIME_RELATIVE, 0, 10); // sleep for 0.00001 seconds
gpioWrite(TRIGGER, 0);
while (gpioRead(ECHO) == 0)
start = time_time();
while (gpioRead(ECHO) == 1)
stop = time_time();
stop=time_time();
measure = (stop-start) *17100.50;
it++;
val=measure;
fprintf(fptr, "%d %d\n", it, val);
gpioTerminate();
pthread_mutex_unlock (&my_mutex11);
}
pthread_exit(0);
}
void *thread2_process( void *arg)
{
for(int j=0; j<20; j++)
{
pthread_mutex_lock (&my_mutex11);
printf("thread2 %d \n", j);
fprintf(gnu, "%s \n","plot 'integers.dat' with linespoints lw 3");
pthread_mutex_unlock (&my_mutex11);
}
pthread_exit (0);
}`
除了显而易见的“如果一个线程总是在等待,为什么要使用两个线程?”之外,您需要做的是在互斥对象中使用某种“state”变量:
// start here
#define STATE_INITIAL 0
// go here when step 1 finishes
#define STATE_STEP1 1
// go here when step 2 finishes
#define STATE_STEP2 2
pthread_mutex_t my_mutex;
int cur_state;
void wait_my_turn(int desired_state) {
pthread_mutex_lock(&my_mutex);
if (cur_state == desired_state) return;
pthread_mutex_unload(&my_mutex);
}
void finish_turn() {
++cur_state;
if (cur_state == 3) cur_state = 1;
pthread_mutex_unlock(&my_mutex);
}
// in main, initialize mutex, lock it, and set cur_state to STATE_INITIAL
// until you are ready for threads to start. Then you need to set it
// to STATE_STEP1 to allow that thread to begin.
// in your threads, begin with wait_my_turn(STATE_STEPn)
// and call finish_turn() when done
…这不是一个很好的例子,但你应该了解它的要点。除了显而易见的“如果一个线程总是在等待,为什么要使用两个线程?”之外,你需要做的是在互斥对象中使用某种“state”变量:
// start here
#define STATE_INITIAL 0
// go here when step 1 finishes
#define STATE_STEP1 1
// go here when step 2 finishes
#define STATE_STEP2 2
pthread_mutex_t my_mutex;
int cur_state;
void wait_my_turn(int desired_state) {
pthread_mutex_lock(&my_mutex);
if (cur_state == desired_state) return;
pthread_mutex_unload(&my_mutex);
}
void finish_turn() {
++cur_state;
if (cur_state == 3) cur_state = 1;
pthread_mutex_unlock(&my_mutex);
}
// in main, initialize mutex, lock it, and set cur_state to STATE_INITIAL
// until you are ready for threads to start. Then you need to set it
// to STATE_STEP1 to allow that thread to begin.
// in your threads, begin with wait_my_turn(STATE_STEPn)
// and call finish_turn() when done
…这不是一个很好的例子,但你应该了解它的要点。谢谢你的回答。
我刚刚找到了同步两个线程的解决方案。必须使用p_线程选项,这是p_thred_cond。您可以按照以下链接上的教程进行操作:
问候。谢谢您的回答。
我刚刚找到了同步两个线程的解决方案。必须使用p_线程选项,这是p_thred_cond。您可以按照以下链接上的教程进行操作:
注意。
两个线程的执行总是随机的。
-如果您不希望这样,请不要运行线程。两个线程的执行不是随机的,它们同时运行。在一起同时,。如果您想一个接一个地运行,可以将它们与互斥体同步。两个线程的执行总是随机的。
-如果您不想这样,请不要运行线程。两个线程的执行不是随机的,它们同时运行。在一起同时,。如果你想一个接一个地运行,你可以将它们与互斥体同步。这不足以确保两个线程都在有限制的时间内运行,但这对于基于条件变量的好解决方案来说只是一半。它只需要所有的条件变量处理。@JohnBollinger是的,我只是想给他们一个足够的例子,让他们朝着正确的方向前进。这并不足以确保两个线程都在有限的时间内运行,但这距离一个基于条件变量的好解决方案只有一半的距离。它只需要所有的条件变量处理。@JohnBollinger是的,我只是想给他们一个足够的例子,让他们朝着正确的方向前进。它绝不是完整的。