Iam locking an object in a function fun1()
and iam using that object in another function fun2() and unlockin it
will the lock still holds good in fun2()
the object is globally declared
The object that iam trying to lock is an array of 3 objects. i want to have access of the object which is not locked from the 3 objects.
these objects are used for establishing a session to the server.
Whenever a new process tries to get access to the server, it should check the available session object, lock it, and unlock after using it
The array of objects are in the shared memory, and the object that is encapsulating the array of objects is a singleton object
Hope this gives you a clear picture to solve my problem
if i lock the entire array, how can i access the other array objects
I need to lock only that element of the array when a session is established with a particular element
ANY object that MAY potentially be access concurrently must be locked before being accessed, and unlocked after the operation.
Concurrently Reading/Writing memory is very nasty. And even when you time stuff yourself, it's possible for the compiler to change the order of instructions when compiling. So locking is important.
If your singleton is returning a pointer to the element. The Singleton's methods of Get/Set/Add/Clear must all have a lock around them to ensure only 1 can be called at a time.
Each element in the array must have locks within it's own encapsulated methods to ensure none are called concurrently.
By the sounds of things though. You want to have a pool of objects to draw from correct?
Lets say you have a array of objects for your connections. When a new connection comes in, the application will loop through the array and find the first object that is free.
In this case, you should have a lock in the object. To ensure when it calls getOkToUse() someone is not calling setOkToUse() at the same time.
Because your singleton is going to be accessed across multiple threads. If any thread is able to modify the contents of the array (e.g add/remove objects) then your array should be wrapped with locks whenever it's accessed.
If you download that it will have a heap of example thread code. Again, I highly recommend you look at this, and some further tutorials on multi-threaded/process development.
I say this as a professional developer who builds multi-threaded applications.