simuloid,
Thanks for posting to the NI Forums. Let me see if I can answer your questions.
1. Is there a way to completely reset the daq from software that clears/resets all tasks?
While it is not possible to clear or stop all running tasks without supplying task handles, it is possible to reset the DAQ device programmatically. You can do this by calling
int32 DAQmxResetDevice ( const char deviceName[] );
if you are developing a C++ MFC application or by calling
Device::Reset();
if you are using the .NET API. This will effectively stop any running tasks since the board will no longer be performing an type of acquisition or generation, however it will not free up software resources associated with the task (i.e. the buffers, configuration parameters, etc.).
While performing a reset may resolve the issue you are experiencing, it seems like something more significant is going on here. From your description it does not sound like you are doing anything that would inherently cause this issue; however, without diving into the code it is hard to say. I think you are correct that this is some type of deadlock, or race condition. Is it possible that your threads are being killed at an inopportune time? Would it be possible to have the shutdown function message the other threads and have the threads end themselves rather than just killing the threads?
2. Will the stopTask fail if the task is waiting for a sample input?
No, as long as the task is running the stopTask should not fail. However, I believe if a DAQmxRead is currently running the stopTask blocks until it has finished executing. This may be where the problem is coming from. If you kill the thread does it halt the execution in the middle of DAQmxRead? I am not as familiar with the Visual C++ and am unsure what the repercussions of killing a thread are. If the DAQmxRead is killed in the middle of execution, I am not sure the behavior of the StopTask is defined.
3. Is there a multi-threaded example?
I could not find any multi-threaded examples written in Visual C++. However, one suggestion would be to keep all the code for a specific DAQ task in the same thread. If you can configure, start, stop and clear the task in a single thread it will remove the possibility for race conditions. Another possibility would be to block the shutdown function until the threads are in an acceptable shutdown state, but this becomes more complicated.
In summary, I believe the way to solve your issue is to move the stop and clear task functions into the threads that are running their respective task. I would then have the shutdown function message the threads to tell them to shutdown. This allows each thread to clean up before exiting and is probably a better practice when programming multi threaded applications anyway.
Hopefully my tips are helpful. Please let me know if you have further questions or would like additional clarification. Good luck with your application and have a wonderful day.
Regards,
Neil S.
Applications Engineer
National Instruments