LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

About the thread safe variable

Hello,

 

I have make 2 projects, the first is using to initialize the enviroument an  loading  some modules, and the 2nd using to execute a special work. In the first project, I use the DeclareThreadSafeScalarVar macro to define the handle of a file, I think the file handle can be shared between 2 processes, and each process can access data by the file handle.It is worth mentioning that the 2nd process loading is by the CreateProcess() API. My question is : if the safe variable is initialized in the first process, can I use the safe variable in the second process? Do I need to reinitialize the safe variable in the 2nd process? Thanks.

 

 

David

0 Kudos
Message 1 of 3
(3,112 Views)

By the way, as the spec if need to share the safe variable, one should use  the "DeclareThreadSafeScalarVar(HANDLE, hLogFile)" macro in an include file. I have did this, but when compiling the project , it shows such an error: "Undefined symbol 'InitializehLogFile' referenced in "xx.c".  What is wrong? 

 

 

David

0 Kudos
Message 2 of 3
(3,109 Views)

David,

 

Thread safe variables are only going to work for threads within the same process. Windows does not allow you to access memory outside of the virtual space of your application. Thus the processes act as if they are unaware of each other's data. Even if you send a pointer to the other application, it will not be able to access the data. There are a few other options for interapplication communication.

 

1. When you are calling CreateProcess() you can pass the new process some command line arguments. If the data you want to send can be serialized, then you can pass it to the other application at start using this command.

 

2. You can pass the data using WM_COPYDATA which allows you to set up a special structure and send it to the other application. This is similar to what you wanted to do with the thread safe variables, except that this data will be read only and must be sent to the other process using something like SendMessage(). This gets quite a bit more complicated.

 

3. You can set up a client server architecture and have all of the data management handled by the server process.

 

From what you have said about your application so far, it may be simpler to just create a single process with two threads. If the applications are dependent on each other, then they likely need to be part of the same process.

National Instruments
0 Kudos
Message 3 of 3
(3,086 Views)