LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Why my global variable is not updated in cvi6 multithreading?

Hi,
My application is multitherading in cvi 6. My one thread is processing in while loop till value of a global variable is set to 0 by other (main) thread. Now, my other thread is setting the global variable to 0 but still it does not get updated for thread in while loop. hence my thread keep on running in infinte loop. How can I indicate to come out when required?
cheers
Niranjan
0 Kudos
Message 1 of 10
(4,101 Views)
Have you tried to use the "volatile" key word with the global var?
I also hat problems with global vars. I fixed it by deklaring

volatile int iEnde;

and using this variable in both threads.

Good luck
Stephan

"Niranjan" schrieb im Newsbeitrag
news:506500000008000000504B0000-1021771306000@exchange.ni.com...
> Hi,
> My application is multitherading in cvi 6. My one thread is processing
> in while loop till value of a global variable is set to 0 by other
> (main) thread. Now, my other thread is setting the global variable to
> 0 but still it does not get updated for thread in while loop. hence my
> thread keep on running in infinte loop. How can I indicate to come out
> when required?
> cheers
> Niranjan
0 Kudos
Message 2 of 10
(4,101 Views)
Niranjan wrote in message
news:506500000008000000504B0000-1021771306000@exchange.ni.com...
> Hi,
> My application is multitherading in cvi 6. My one thread is processing
> in while loop till value of a global variable is set to 0 by other
> (main) thread. Now, my other thread is setting the global variable to
> 0 but still it does not get updated for thread in while loop. hence my
> thread keep on running in infinte loop. How can I indicate to come out
> when required?
> cheers
> Niranjan

Could be you have two instances of a global with the same
name?
Are both places the variable is used in the same C file?
If not are they defined as a variable in one C file and as
an extern variable in the other?

Paul
0 Kudos
Message 3 of 10
(4,101 Views)
Hi Niranjan,

In the multithreading library provided by CVI, there is one function that allows you to create thread safe variables. Variables that will be accessed by multiple threads. CVI takes care of the synchronization for you and you don't have to worry about declaring them globally.

There is a shipping example that shows how to create them and use them in a multithreaded program.
Check out the project in the folder /samples/utility/Threading/ThreadSafeVar

Regards,
Azucena
NI
0 Kudos
Message 4 of 10
(4,101 Views)
Hi,
Thanks to all of u for ur replies. I tried using volatile alongwith variable declaration. But it didn't work.
I did study ThreadSafeVar example given by NI. I used DefineThreadSafeScalarVar function and i am getting run time error "Librray function error (return value == -14901) [0xffffc5cb] Invalid handle."
Now there is no help available on this function. Does anybody know what is this error for?
Here is line from my code which is giving error:

DefineThreadSafeScalarVar(int, TsvDclamp, 0);

cheers
niranjan
0 Kudos
Message 5 of 10
(4,101 Views)
I used the same line of code you used and was able to use the TsvDclamp variable between two threads. Do you have several source files where you access the variable? I'm out of ideas as to why you would get an invalid handle error in this macro.
If you want, I can take a look at a stripped down version of your program that exhibits the same error.

Regards,
Azucena
0 Kudos
Message 6 of 10
(4,101 Views)
Azucena,
I am attaching file test1.zip containing a cvi project and related files. Can you please run it and have a look at the error message I am getting?
Thanks
Niranjan
0 Kudos
Message 7 of 10
(4,101 Views)
I believe the zip file is missing the file DClamp.h.
Can you send it to me? Otherwise I can't build the project.

Azucena
0 Kudos
Message 8 of 10
(4,101 Views)
Azucena,
I am extremely sorry. I apologize. I am attaching test1.zip again with DClamp.h.
Thanks
Niranjan
0 Kudos
Message 9 of 10
(4,101 Views)
Okay, I see the problem. The threadsafeVar needs to be initialized before it can be used. You need to call
InitializeTsvDclamp () before any thread can use it.
You can do this right after loading the panel to be safe.

Good luck,
Azucena
0 Kudos
Message 10 of 10
(4,101 Views)