Using Locks in a function

hai plz help me

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

consider the following scenario

object obj;
void fun2()
{
fun1();
obj.display();
unlock();
}

obj& fun1()
{
lock();
return obj;
}

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

plz help me its urgent

Thanks
Vadan
should I use a lock for each object in the array or a single lock is sufficient?
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.


You have to lock the entire array. Unlock it when your finished with the object in question.
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
Its like searching for an object within the array that is free and accessing it by locking it
You need to lock the array while searching. Unlock the array once you have removed a reference for your object.

Then your object must lock on each call to ensure no 2 threads get the same object and try to work on it con-currently.
Sorry I did not get you clearly

let us say i have the following array

class1 arr[3];

all the 3 objects/elements of the array should be accessed concurrently
this object (arr[3]) is encapsulated in singleton object

as i explained before, each object/element of the array is used for establishing a session to the server

can you elaborate taking this example, or may be with the code

sorry for asking you too detail, as iam not getting the exact point here

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?
yes i want to have a pool of objects, from which i should access them concurrently

so from what i understand, each element of the array must have a lock
http://en.wikipedia.org/wiki/Object_pool

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.
Thanks Zaita for all your valuable suggestions
I would hardly call them "suggestions", more guidelines you need to follow to have bug-free multi-threaded code.

I would HIGHLY suggest you look at using a 3rd party threading library. The Boost library is a good one, and is multi-platform (bonus eh).

http://www.boost.org/doc/libs/1_35_0/doc/html/thread.html

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.
Topic archived. No new replies allowed.