11-08-2021 04:22 PM
Hi all!
My apologies to you in my ignorance. I'm a Linux developer.
I have a worker thread that I need to run wide open. If, however, there is nothing for it to do, I need it to sleep until an asynchronous event turns it back on. In Linux, I might do something like:
wait_queue_head_t wq;
atomic_t data_present = ATOMIC_INIT(0);
int main(void)
{
init_waitqueue_head (&wq);
// Create the worker_thread
// Provide the callback for the async event
// wait for some event to make us return.
return 0;
}
int isr_callback(<whatever>)
{
// Handle the event, and post the data for the worker thread
atomic_set(data_present, 1);
wake_up(wq);
}
void worker_thread()
{
while (!stop_worker_thread){
wait_event(wq, atomic_read(data_present));
// work the data.
atomic_set(data_present, 0);
}
}
How do I achieve the same thing in LabWindows? To be more wordy: I want to generate a thread that handles LOADS of information. However, if the system goes dark for a bit, I don't want that thread to spin but rather sleep waiting on someone else (the ISR) to wake it up. Also, given the bulk of the data coming in, creating a thread on the fly would overload the system and would cause the application to crash -- so, I need only one thread to react to an async event that I can then wake up on to process data.
Hope this is a clear question.
Thanks in advance for any direction you can provide!
Andy
11-09-2021 10:23 AM
If you have stumbled across this thread and were hoping to find the answer, the best I came up with after an all day search was <https://docs.microsoft.com/en-us/windows/win32/sync/using-condition-variables>. This appears to be the "right way" to do it.
11-09-2021 10:33 AM
You're right, your application seems to fit well in the producer-consumer paradigm. If you want to explore this way you could benefit by using the Tread-Safe Queues that CVI offers in the Utility library: until the producer does not input data in the queue the consumer thread is idle and consumes no system resources.
I suggest you to read Creating Applications >> Creating Multithreaded Applications chapter in CVI help where the thread safe queue mechanism is described, and the examples that you can find searching for Queue in the Example Finder (Help >> Find Examples...)