Boost logo

Boost Users :

From: Dhanvi Kapila (boostguy_at_[hidden])
Date: 2006-02-17 12:58:11


Malte Clasen wrote:
> Dhanvi Kapila wrote:
>
>> I have this piece of code for mutex operations. is this code
>> thread-safe ? I plan to use this code in threads...
>> ------------------------------------
>> class BoostMutexLock
>> {
>> public :
>> void lock() {
>> lk = new boost::mutex::scoped_lock(global_mutex);
>> }
>> void unlock() {
>> delete lk;
>> }
>> BoostMutexLock() : lk( 0 ) { }
>> ~BoostMutexLock() { }
>>
>> private:
>> boost::mutex global_mutex;
>> boost::mutex::scoped_lock* lk;
>> };
>> ------------------------------------------------------
>>
>
> That depends on how you want to use this object. I assume that you want
> to share it between threads since there's no way to let two locks use
> the same mutex with this design.
>
Thats a very valid point. Two threads cannot share this lock object
since each thread would have its own mutex object.
This negates the whole point of achieving to do synchronization. But if
I declare the mutex as static then all the threads would share just one
mutex object and that too is something I dont want to do.

I need to wrap boost mutexes in a interface so that I can abstract that
at my application level. My application doesnt need to know how the
locking is done.

How would a shared_ptr help me in such a scenario ??

Thanks for your time and responses.. I really appreciate all the help.

Dhanvi


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net