简体   繁体   中英

Multi threading using p thread Linux problem

I have two functions, producer and consumer called by two p threads, but the while loop in the function is not running. Its a linux RT system. this is my code. im coding in 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);

 }

Should i use pthread_exit or pthread-join? how to use pthead_exit to exit first thread when while loop has exited?

now my console prints just this

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

but actually value of i and k should go to 1000 and when it reaches 1000, while loop will stop and exit

Sometimes im getting this error too

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

You need to call pthread_join in the main function after creating thread1 and thread2. Otherwise, the main thread will terminate before thread1 and thread2 are completed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM