LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multhreading (in fact there are two threads).


One thread terminating the execution of the
other. How to do that? I appreciate an example. I have a thread which runs my test procedure (thread runtest function),I want the user to click a stop button(main thread) and I want to
finish the execution of the other thread.
Does anybody have an example that I can take a
look? I start the "run thread function" when
the user push the start button on the interface.

Thanks in advance,
Jefferson Muniz
0 Kudos
Message 1 of 4
(3,478 Views)
Hi,


Have you had a look at the example in the CVI\samples\apps\daqmthread

Regards
Ray
Regards
Ray Farmer
0 Kudos
Message 2 of 4
(3,478 Views)
I'd recommend to do this in following way :
Define a global variable that indicates if runtest should stop.
e.g.
volatile int stopThread;
Set this variable to false when you start runtest.
When the user clicks the stop button set this variable to true and then wait
for the ending of the thread (using
CmtWaitForThreadPoolFunctionCompletion()).
In runtest frequently check the variable and end the function when
stopThread is true.
Sounds simple and is simple but its the securest way to do it.

Hope that helps,
Manfred Schininger

jeffmuniz@zipmail.com.br schrieb in im Newsbeitrag:
5065000000080000004D1B0000-986697009000@quiq.com...
> Multhreading (in fact there are two threads).
> One thread terminating the execution of the
> other. How to do that? I a
ppreciate an example.
>
> I have a thread which runs my test procedure (thread runtest
> function),I want the user to click a stop button(main thread) and I
> want to
> finish the execution of the other thread.
> Does anybody have an example that I can take a
> look? I start the "run thread function" when
> the user push the start button on the interface.
>
> Thanks in advance,
> Jefferson Muniz
0 Kudos
Message 3 of 4
(3,478 Views)
This is a good question. You might think that you could do it by throwing an exception from one thread into the other (kind of like the "kill" signal in Unix C or Posix implementations), such that the second thread will then "know" to terminate itself. But you can't do this in Win32 (and not in a CVI app). You might think you could just kill off the second thread with a Win32 SDK call, but if you arbitrarily terminate it (e.g. with the TerminateThread Win32 SDK call) you run the risk of killing the thread when it's in an inappropriate place (such as inside of a critical section) and then you're hosed. Micro$soft calls TerminateThread "a dangerous function" 🙂

So, like the other developer has said, probably the best way is for the second thread to periodically po
ll a flag variable or some other inter-thread communication mechanism to see if the first thread wants it to quit.

What a hassle, huh? This is a well-known weakness of Win32. Some languages/OS's provide for this, but Win32 doesn't, so NI couldn't implement such a facility - you need help from the OS kernel to do it and Win32 just won't do it. So, you have to clutter your code with polling code that's a real annoyance. Even if you know that the second thread is never in a critical section that you've written, if you kill it when it's inside an NI DLL, the same thing might happen, 'cause you don't know how they did things. I've had this happen to me with the NI DAQ DLL. And the same issue exists for one process killing off another - not the critical section issue - but rather shared code and mechanisms - such as DLL's - can get tangled up.
0 Kudos
Message 4 of 4
(3,478 Views)