06-23-2010 04:13 AM - edited 06-23-2010 04:20 AM
Hi all, I need some clarifications about locks in CVI. CmtNewLock creates a lock to be used in multithreaded applications. I am a little puzzled about the first parameter of this function -Lock Name- which according to the online help is:
CmtNewLock <script src="launchhelp.js"></script>
lockName | const char * | The name you want to give to the lock you are creating. Pass NULL if you do not want to name the lock. Unnamed locks provide better performance than named locks. You must name the lock if you plan to use the lock across multiple processes. The lock name must not be longer than MAX_PATHNAME_LEN characters. |
An extended search in this board has found little or no specific posts on this parameter. The most relevant is this post by Chris which is somewhat confusing: he says "iit reserves ownership of the thread lock, which is named lock in this case" nevertheless the code included uses NULL as the lock name.
I almost always used unnamed lock in my multithreaded applications (lock name = NULL) but I wonder what the"multiple processes" the online help refers to are?
And what can be the overhead introduced by naming the lock?
06-23-2010 07:42 AM
It looks to me like the 'lock' mechanism in CVI is a wrapper for the Mutex object (in Windows). Named Mutex objects allow threads across different processes (not just threads in one process) to synchronise: the name is used to enable the different processes to access the same Mutex object.
If the CVI library is super-efficient it may or may not be using the Windows Critical Section object in the case of an unnamed lock, which is a slightly more efficient mechanism than the Windows Mutex for threads within a single process.
06-23-2010 11:18 AM
Roberto,
You can use objects like a mutex or shared memory blocks to facilitate inter-process communication for passing data between different applications. It's not an approach I would personally use but it is definitely supported under Windows. The name parameter is how different applications can access those shared objects. Without a name, only the application that creates the object can use it.
I hope that helps.
06-24-2010 02:04 AM
Ok, so till I am dealing with single-process-multiple-threads application I can continue using unnamed locks, with the benefit of a better efficiency.
I'm not planning to run interprocess communication / syncronization so I will stay in this scenario.
Thanks all.
06-24-2010 02:56 PM
Exactly Roberto.
If you aren't going to synchronize to another application, you've got it.