06-16-2008 01:12 PM
06-17-2008 11:57 AM
Hello coanda,
Thanks for posting on the NI forums. Let me start by saying that you’re quite right. CmtGetLock() does not put a lock on any variable, but instead it reserves ownership of the thread lock, which is named lock in this case. An attempt to take ownership of the thread lock from a separate thread must wait until the owning thread releases the lock using CmtReleaseLock(). In other words, if thread 1 reaches the CmtGetLock() function before thread 2 does, then thread 2 suspends execution until thread 1 reaches CmtReleaseLock(). Once the lock is release by the first thread, the second thread can then reserve ownership of the lock, and continue execution.
So, if you desire to protect multiple global variables in different threads, you must only modify these variables when ownership of a lock is activated. It is up to the developer to coordinate which lock is reserved when accessing a certain variable. The important thing to understand is that there is never any lock put on a variable, rather ownership of locks themselves are reserved and released. If we wanted to protect another global variable in the example from the above post named count2, then the modified code would look like the following:
-----
int lock;
int count;
int count2;
int main (int argc, char *argv[])
{
int functionId;
CmtNewLock (NULL, 0, &lock);
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, ThreadFunction, NULL, &functionId);
CmtGetLock (lock);
count++;
count2++
CmtReleaseLock (lock);
CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, functionId, 0);
CmtDiscardLock (lock);
}
int CVICALLBACK ThreadFunction (void *functionData)
{
CmtGetLock(lock);
count++;
count2++;
CmtReleaseLock(lock);
return 0;
}
-----
06-17-2008 12:11 PM
06-18-2008 11:17 AM
06-19-2008 05:21 PM
Hello wally_666,
Thank you for your questions as well. Functionally speaking, CmtGetLock() and CmtReleaseLock() are no different than the Windows SDK EnterCriticalSection() and LeaveCriticalSection() functions. When calling Windows SDK functions from LabWindows/CVI, one must include the proper header files and libraries in order to access the Windows SDK functions. Using the CmtGetLock() and CmtReleaseLock() functions from LabWindows/CVI eliminates this step, and also reduces compile time. For more information on calling Windows SDK functions from LabWindows/CVI, see the following link: Using the Windows SDK in LabWindows/CVI
As far as using the functions during runtime on a Linux machine, the LabWindows/CVI Run-Time Module for Linux supports these two functions, and therefore can be executed on a Linux machine.
Lastly, to update the current email address in your NI User Profile, follow these steps:
(1) Log in to your profile
(2) At the top of a discussion forum thread, click on the My Profile link, located just under the thread category (e.g. “LabWindows/CVI”)
(3) On the Personal Profile tab of your NI User Profile, click on the Modify your NI User Profile link at the top of the tab.
(4) Once the My Profile page pops up, click on the Modify Profile link, which is the third link down on the page.
(5) On the Modify Profile page, you can change your email address in the textbox under the Enter your email address: heading.
06-24-2008 09:02 AM