May not be what you want.
Can you Identify what the scope is on this Lock() call?
Essentially a mutex m, is either locked, or unlocked.
The OS core, is not selective about who acquires the mutex, when.
So lets say that you are in a multi threaded environment, as is the case when synchronization is required, and you have a mutex, which is inherently associated with the object / instance.
So when you call Lock(), you are in essence calling ParentClass.Lock().
Which means that accessors for other data elements are waiting for exclusive access to that object instance before they can write.
If mutlple calls come in for seperate data members it wont matter. And since the OS is not selective, one accessor can be held indefinately while other accessors do there jobs. Potentially dogging your system. And takeing away any advantage you hoped to gain by being multi-Threaded.
A better way is to create a Critical Section for each Specific Data Item or member. So that calls to that accessor is only blocking other calls to that same accessor. and the Other Accessors can freely access their data simultaneously.
So, to speed up your application serialize the accessors for each data member, instead of serializing access to the intire object instance.
- The steps are easy ...
-
-
1. at instantiation call InitializeCriticalSection() for each data member.
-
2. in the accessors call EnterCriticalSection() emmediatley prior to the access,
-
3, call LeaveCriticalSection() immediately following access, and finally
-
4. DeleteCriticalSection in class Destructor.
-
-
1: http://msdn.microsoft.com/en-us/library/ms683472(VS.85).aspx
-
2: http://msdn.microsoft.com/en-us/library/ms682608.aspx
-
3: http://msdn.microsoft.com/en-us/library/ms684169(VS.85).aspx
-
4: http://msdn.microsoft.com/en-us/library/ms682552(VS.85).aspx
Which is much faster than the equivalent mutex operation.
BTW the Lock keyword is a C# thing, that was extended by the CLR.
Without the CLR your application may have issues.
Happy Coding!