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;
}

plz help me its urgent

Thanks
Vadan

Recommended Answers

All 5 Replies

depends on what lock() and unlock() do -- they are not standard c or c++ functions. If you use a semiphore, they are valid throughout a thread, not just the function in which they were used. I suspect lock() and unlock() does that too.

Threads, not functions, lock and unlock access to objects.

Probably with such diffuse lock/unlock logic you will have deadlock troubles sooner or later...

Actually 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

should I use a lock for each object in the array or a single lock is sufficient?

You might need two locks -- one for the entire array and another for each element of the array. First lock the array itself, lock the array element, then unlock the array so that another thread can use the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.