LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

passing data between two threads apart from the main thread

Does anyone know how to use a ThreadSafeQueue to pass data between Thread #1 and Thread #2, where Thread #2 is not the main thread.  For example, I would like to have a Data Acquisition Function in one thread, an Analysis Function in a second thread, and the user interface in the main thread.  The analysis function would be the callback of the ThreadSafeQueue#1.  The example programs all use a variation of this:

 

CmtInstallTSQCallback (tsqHdl, EVENT_TSQ_ITEMS_IN_QUEUE, ANALYSIS_LENGTH, AnalysisCB, NULL,

                                       CmtGetCurrentThreadID(), NULL); 

 

How do I ensure that CmtGetCurrentThreadID() from the example above is from the AnalysisThread and not the main thread?

 

A second question I have, is how do I pass a 2D array in a ThreadSafeQueue?

 

Thanks in advance for any help.  -Philip

0 Kudos
Message 1 of 6
(3,451 Views)

You can call CmtInstallTSQCallback at the entrance in the thread function, before you start looping to process events in the thread (condition necessary for the TSQ mechanism to work, see the online help for Callback Thread ID parameters in the function).



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 6
(3,446 Views)

With reference to the 2D part of your question, you cannot pass a 2D array in a TSQ as is, you can either line vectors one after the other and pass them in the queue or create a structure with elements in one row of your matrix and pass an array of structures in the queue: the reader callback will need to rebuild arrays from structure elements.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 6
(3,444 Views)

If one of the dimension of your 2D array is small, like 2 x N, then maybe you can create 2 TSQs 😉

 

So, you can write one row to the first TSQ and second row to the second TSQ.

S. Eren BALCI
IMESTEK
0 Kudos
Message 4 of 6
(3,433 Views)

Thanks Roberto-

This does make sense to me; however, the problem I am running into when I try to do this is as follows: 

 

My understanding is that for a function to be started as a separate thread, it must be of the format:

 

int     CVICALLBACK DAQThreadFunction (void *functionData);

 

but for it to be the callback of a thread safe queue, it must be in the format:

 

void    CVICALLBACK AnalysisCB(CmtTSQHandle queueHandle, unsigned int event, int value, void *callbackData);

 

 

So I want AnalysisCB to be both the thread safe queue's callback, but also in its own thread.

 

I tried to do this with the following code:

 

   status = CmtScheduleThreadPoolFunctionAdv (DEFAULT_THREAD_POOL_HANDLE, AnalysisCB, NULL,                             THREAD_PRIORITY_ABOVE_NORMAL, NULL, 0, NULL, 0,&analysisCBFunctionID);

 

 

and then calling the CmtInstallTSQCallback inside AnalysisCB as you suggested above, but I get the following error:

 

 

 192, 94   Type error in argument 2 to `CmtScheduleThreadPoolFunctionAdv'; found 'pointer to __cdecl void function(CmtTSQHandle,unsigned int,int,pointer to void)' expected 'CmtThreadFunctionPtr'.

 

To summarize:

 

I want the function DAQThreadFunction to be in its own thread and to control the data acquisition.  I then pass the acquired data through TSQ1 to the AnalysisCB, which should also be in its own thread.  Then I want to pass the resulting processed data through TSQ2 to the main thread for display.

 

Is this possible?

 

0 Kudos
Message 5 of 6
(3,407 Views)

Obviously the same function cannot be used as a thread function and as aTSQ callback. The structure I had in mind was a simple thread function that installs the TSQ callback and then loops processing events and doing nothing more. All thread operations are handled inside the TSQ callback, which receives and handle data.

 

The conceptual framework is the following:

Thread function
{
  Install TSQ callback

  while (threadExecuting) {
     ProcessSystemEvents ();
  }

  Properly dispose of resources and functions

  return;
}



TSQ callback
{
Read data from the queue
Process data
send results on the second TSQ
}

 

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 6
(3,396 Views)