01-16-2017 03:14 AM
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
Solved! Go to Solution.
01-16-2017 03:41 AM
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.
01-16-2017 05:08 AM - edited 01-16-2017 05:13 AM
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.
01-16-2017 05:23 AM
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?
01-16-2017 05:41 AM - edited 01-16-2017 05:42 AM
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.
01-16-2017 05:55 AM
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?
01-16-2017 06:24 AM
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.