NI Linux Real-Time Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

Threading issues using std::thread on cRio9068

Solved!
Go to solution

Hi All

As some might know from previous posts I've been building a project to record serial data and high frequency recordings from two NI 9222 DAQ cards.

I've come across an issue with threads in C++ which does not make any sense to me and would be grateful fro some advice on. Attached is a zipped eclipse workspace containing a project called cRioTestC. This project consists of four main components.

1) NiFpga.h/.c - generating from C API Generator to run FPGA.

2) ReadNIDAQ.h/.cpp - contains higher level functions to run FPGA on one thread, save data in a buffer and then write to disk on another thread.

3) WriteWav.h/.cpp- contains higher level functions to save data in .wav file format using libsndfile.

4) ReadSerial.h/.cpp- contains functionality to time stamp and save data from serial ports.

I've used the the thread library successfully in ReadNIDAQ to rapidly read FIFO data and pass data to a buffer in memory before saving on another thread. (See record_DAQ() function). However recently I tried to do the same for serial data and keep getting a segmentation fault. Originally I thought this might be due to some coding error somewhere in the ReadSerial source file and so I simplified code until I could isolate the issue. However I'm now at a stage were I've simplified so much I simply have the following code in the main() method.

/* Includes high level functions for reading DAQ card*/

#include "ReadNIDAQ.h"

/*Includes high level functions for reading Serial port */

#include "ReadSerial.h"

/* Required for console interactions */

#include <iostream>

#include <thread>

void hello(){

    std::cout << "Hello from thread " << std::endl;

}

int main(){

    /*Test read serial port data*/

//  record_Serial(0,B9600 );

//  thread serial_t1(record_Serial, 0, B9600);

//    usleep(30000);

   

    std::thread t3(hello);

    t3.join();

//    /*Test write .wav file*/

//    createSoundFile(8, 42000);

//    testWavWrite();

//    /*Define the read and write threads and begin recording*/

//    record_DAQ();

    return 0;

}

This is the simplest of simple for creating a thread but a segmentation fault occurs->

admin@cRioKE:~# echo $PWD'>'

/home/admin>

admin@cRioKE:~#

admin@cRioKE:~# /home/admin/cRioKE/cRioTestC;exit

Segmentation fault

logout

I've tried on two systems and same thing occurs. This doesn't make any sense to me as threads work fine in the ReadNIDAQ.h function. (uncomment the record_DAQ() and it should work if FPGA bitfile has been transferred to cRio). Looking on the Internet some people say that gcc4.4. should not be used with std::threads.

My question is there a fundamental issue here or have I done something stupid? I'm relatively new to C so stupid is definitely a possibility.

Note: I've modified two main things from standard cRio setup 1) installed libsndfile and 2) changed build options so that threads library works. 

Touch wood this is the last hurdle in this project so all help very much appreciated! All code is NERC funded therefore open source although this project is obviously currently incomplete.

Cheers.

0 Kudos
Message 1 of 4
(3,901 Views)
Solution
Accepted by topic author jamie_mac

I played around with gcc 4.7 on the target and getting std::threads to work properly and was unable to do so with a quick jab, I would really recommend using the existing pthread functionality (considering the fact that that's what's being used under the hood anyway)

0 Kudos
Message 2 of 4
(3,064 Views)

OK, tried with pthread and all seems to work. Guess threads is just too new- much simpler way to code though.

Just a reminder for anyone reading this you need to

1) add -pthread to build options: In eclipse:  project->properties->C/C++ Build->Settings->Tool Settings -> Cross G++ Compiler->Miscellaneous->Other Flags should be '-c -fmessage-length=0 -pthread'

2) add -pthread library In eclipse:  project->properties->C/C++ General->Paths and Symbols->Libraries -> Add...-> add 'pthread' to text box and click OK.

3) Obviously use #include <pthread.h> in code

0 Kudos
Message 3 of 4
(3,065 Views)

jamie_mac,

Glad to hear that you were able to get something working for your use case. And it is a shame that the cleaner C++11 stuff was not quite usable yet, but pthreads have been around forever so they are tested more thoroughly and will work without issue (and, besides, the c++11 implementation is done atop pthreads anyway). Keep an eye on trying it with later releases of the toolchains (although, as I found, even 4.7 has some rough spots, I didn't try as thoroughly as you did). Thanks for providing some details on how you got things to work with the Eclipse IDE as well.

0 Kudos
Message 4 of 4
(3,065 Views)