LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

PostDeffered Calls and Multithreading

Solved!
Go to solution

Hi,

 

Can someone give a brief idea about post deffered calls and use of the same in multithreaded applications. Is it like calling the same function from multiple threads? In that case, whether the memory space used by the function is samewhen called by different threads? Whether a call to such function will block calls to the same function from other threads at the same time?

 

Best Regards

Deepu Jacob

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 1 of 7
(3,522 Views)

Based on my experience you must add some extra code (locks, semaphores or something like that) to be 100% sure that the function is not called at the same time by different threads.

As far as I know the CVI built-in mechanism for PostDeferred Calls is not thread-safe by itself.

But I can be wrong, since I had this experience with old CVI (2010) and I used PostDeferred to call DAQmx functions.

Vix
-------------------------------------------
In claris non fit interpretatio

-------------------------------------------
Using LV from 7
Using LW/CVI from 6.0
Message 2 of 7
(3,515 Views)
Solution
Accepted by topic author djac91

PostDeferredCall is not strictly related to single- or multi-threading: it's a way to schedule a function to be executed when the system is free. As an example, if you are in a loop processing some data, you may PostDeferredCall a function that shows results. The function will be executed automatically at the next event processing (normally after the loop ends, but be careful if you have some ProcessSystemEvents embedded somewere in your loop).

It is important to understand that the deferred function is executed in the main thread, so if you are using it in a multithreaded environment you know that the function is not influencing the other threads (but you may need to protect your data if the deferred function and the threads are manipulating the same set of data). In a multithreaded application, you may use PostDeferredCall to periodically issue a function in the main thread that shows the status of the application, without disturbing other threads activity and scheduling.

 

Since all deferred functions are executed in the same thread (the main one) they should be scheduled one after the other, unless they call ProcessSystemEvents. There is no guarantee, though, neither on the actual time nor on the order when they will be executed.



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?
Message 3 of 7
(3,499 Views)

Hi,

 

Thanks vix and roberto for you comments.

 

What about the reentrancy of the function?. Assume that i have a simple function to calculate sum of two numbers (no globals used), and i'm calling the sum function from two threads simultaneously. In that case, does each of the thread has to wait till the other thread completes execution of the sum function?

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 4 of 7
(3,495 Views)

There no syncing mechanism implicit in PostDeferredCall, treat it as a fight-and-forget missile: PostDeferredCall () returns immediately and caller thread will continue processing as if the call hadn't been issued.

And since deferred functions are all executed in the main thread, variables in them are not accessed by different threads at the same time so they normally don't need to be protected.

 

If you need to wait for the result of the deferred call before continuing use PostDeferredCallToThreadAndWait instead.

 



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 5 of 7
(3,490 Views)

Thanks for bringing more clarity into postdeffered calls.,

 

But what if it is a normal function call instead of post deffered call. In that case the thread should definitely wait for the function to complete. can two threads call such a function simultaneously?

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 6 of 7
(3,485 Views)

Yes, two threads can call the same function at the same time, and the OS guarantees an indipendent memory space and stack for each of them. That is, variables local to a function are independent from thread to thread.



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?
Message 7 of 7
(3,478 Views)