【问题标题】:Multi threading using p thread Linux problem多线程使用 pthread Linux 问题
【发布时间】:2020-07-23 06:52:39
【问题描述】:

我有两个函数,生产者和消费者,由两个 p 线程调用,但是函数中的 while 循环没有运行。它是一个linux RT系统。这是我的代码。我在eclipse中编码。

#include <stdio.h>
#include"NIDAQmx.h"
#include <pthread.h>
#include "sts_queue/s_q.c"
#include <stdlib.h>

void *producer(void *ptr);// first function
void *consumer(void *ptr);// second function

TaskHandle taskHandle = 0;
int ret = 0;
int numChannels = 0;
int numRead;
float64 data[100];
int iret1, iret2;
pthread_t thread1, thread2;

int main(void) {
char *message1 = "Producer ended";
char *message2 = "consumer ended";
    init();
ret = DAQmxCreateTask("task", &taskHandle);

ret=DAQmxCreateAIVoltageChan(taskHandle, "PXI1Slot2/ai0", "",
        DAQmx_Val_Cfg_Default, -5, 5, DAQmx_Val_Volts, NULL);

ret=DAQmxCfgSampClkTiming(taskHandle, "", 1000, DAQmx_Val_Rising,DAQmx_Val_ContSamps, 100);

ret=DAQmxGetTaskAttribute(taskHandle, DAQmx_Task_NumChans, &numChannels);


ret=DAQmxStartTask(taskHandle);

iret1 = pthread_create(&thread1, NULL, producer,(void*) message1);// calling two threads
iret2 = pthread_create(&thread2, NULL, consumer,(void*) message2);// calling thread



}

void *producer(void *ptr) // enque function
{
char *message;
int i = 0;
int ret;
message = (char *) ptr;
while(i<1000)
{
//ret=DAQmxReadAnalogF64(taskHandle, 100, 10.0, DAQmx_Val_GroupByChannel, data,100 * numChannels, &numRead, NULL);
printf("task handle=%d\n",taskHandle);
printf("value of i=%d\n",i);
printf("Number of sample read%d\n",numRead);
printf("ret%d\n",ret);
sleep(.1);
i++;
}
ret=DAQmxStopTask(taskHandle);

ret=DAQmxClearTask(taskHandle);

printf("%s \n", message);
pthread_join(thread1, NULL);
return 0;
}

void *consumer(void *ptr) // deque function
{
char *message;
int k = 0;
int elements=0;
message = (char *) ptr;
while(k<1000)
{

    printf("value ofk=%d\n",k);
    sleep(.1);
    k++;
}
printf("%s \n", message);
pthread_join(thread2, NULL);

 }

我应该使用 pthread_exit 还是 pthread-join? while循环退出时如何使用pthead_exit退出第一个线程?

现在我的控制台只打印这个

task handle=-163491360
start0
value ofk=0
task handle=-163491360
value of i=0
Number of sample read0
ret0
logout

但实际上 i 和 k 的值应该达到 1000,当达到 1000 时,while 循环将停止并退出

有时我也会遇到这个错误

pure virtual method called
terminate called without an active exception
Aborted
logout

【问题讨论】:

    标签: c multithreading pthreads nidaqmx


    【解决方案1】:

    创建thread1和thread2后,需要在main函数中调用pthread_join。否则,主线程将在thread1和thread2完成之前终止。

    【讨论】:

    • 我应该使用 pthread_join 还是 pthread_exit?退出这个线程?并且我的 void main() 是否应该有一个 while 循环,以使其保持活动状态直到我的线程退出?
    • 您必须从 main 函数调用 pthread_join 才能等待其他线程完成。 main 中的 while 循环不是必需的,因为 pthread_join 确保主线程保持活动状态,直到其他线程完成。
    • 如果您在 main 结束时调用 pthread_exit,主线程(和您的程序)将继续在后台运行,直到所有其他线程完成但您不会看到输出。见stackoverflow.com/a/3559873/4865273
    • pthread_join返回时,分配的资源将被释放,因为这意味着线程已被销毁。
    • @tomptz 不,诸如动态分配的内存之类的资源不会在线程退出时被销毁,除非该线程是进程中的最后一个线程,(然后操作系统会清除所有未与另一个共享的过程)。
    猜你喜欢
    • 1970-01-01
    • 2019-04-12
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多