
Hello. I'm designing a class with a locking policy. I have some code that does exaclty this: if (cond) { locking_policy_.lock(); out << message.message(); locking_policy_.unlock(); } return *this; And the lockingpolicy is a class like this one: struct ThreadSafe { boost::mutex io_mutex_; boost::unique_lockboost::mutex lock_; ThreadSafe() : io_mutex_(), lock_(io_mutex_, boost::defer_lock) {} void lock() { std::cout << "Locking from " << boost::this_thread::get_id() << std::endl; lock_.lock(); } void unlock() { std::cout << "Unlocking from " << boost::this_thread::get_id() << std::endl; lock_.unlock(); } }; Then, I create 3 threads and join them in the same order I created them: boost::thread t1(&doSomething, "Hello1", boost::ref(logsink)); boost::thread t2(&doSomething, "Hello2", boost::ref(logsink)); boost::thread t3(&doSomehting, "Hello3", boost::ref(logsink)); t1.join(); t2.join(); t3.join(); and I get a lock_error. Any help here, please? I can't figure out the problem.

On Mon, Apr 4, 2011 at 3:17 PM, Germán Diago
Hello. I'm designing a class with a locking policy. I have some code that does exaclty this:
if (cond) { locking_policy_.lock(); out << message.message(); locking_policy_.unlock(); } return *this;
And the lockingpolicy is a class like this one:
struct ThreadSafe { boost::mutex io_mutex_; boost::unique_lockboost::mutex lock_;
ThreadSafe() : io_mutex_(), lock_(io_mutex_, boost::defer_lock) {}
void lock() { std::cout << "Locking from " << boost::this_thread::get_id() << std::endl; lock_.lock(); }
void unlock() { std::cout << "Unlocking from " << boost::this_thread::get_id() << std::endl; lock_.unlock(); } };
Then, I create 3 threads and join them in the same order I created them:
boost::thread t1(&doSomething, "Hello1", boost::ref(logsink)); boost::thread t2(&doSomething, "Hello2", boost::ref(logsink)); boost::thread t3(&doSomehting, "Hello3", boost::ref(logsink));
t1.join(); t2.join(); t3.join();
and I get a lock_error. Any help here, please? I can't figure out the problem.
Hi! Could you please provide the doSomething implementation and how ThreadSafe is used in the whole picture? Currently I don't understand the benefit of lock_ being a member variable. BTW: writing to std::cout from multpile threads without synchronisation will produce "more or less" race conditions. Streams will still be in a consistent state, but the output is anything else than expected. Most std streams implementations are synchronized for single characters. Therefore you might end up having the partial output from one thread and partial output from other threads within a single line . With Kind Regards, Ovanes

2011/4/4 Germán Diago
Hello. I'm designing a class with a locking policy. I have some code that does exaclty this:
if (cond) { locking_policy_.lock(); out << message.message(); locking_policy_.unlock(); } return *this;
And the lockingpolicy is a class like this one:
struct ThreadSafe { boost::mutex io_mutex_; boost::unique_lockboost::mutex lock_;
ThreadSafe() : io_mutex_(), lock_(io_mutex_, boost::defer_lock) {}
void lock() { std::cout << "Locking from " << boost::this_thread::get_id() << std::endl; lock_.lock(); }
void unlock() { std::cout << "Unlocking from " << boost::this_thread::get_id() << std::endl; lock_.unlock(); } };
Then, I create 3 threads and join them in the same order I created them:
boost::thread t1(&doSomething, "Hello1", boost::ref(logsink)); boost::thread t2(&doSomething, "Hello2", boost::ref(logsink)); boost::thread t3(&doSomehting, "Hello3", boost::ref(logsink));
t1.join(); t2.join(); t3.join();
and I get a lock_error. Any help here, please? I can't figure out the problem.
You used the *same* lock to lock & unlock the mutex in different threads, each thread should use its own lock.

You used the *same* lock to lock & unlock the mutex in different threads, each thread should use its own lock.
I didn't realize I was doing that wrong. I usually use lock_guards, which are used inside functions, Then, I have a lock for every thread. This way I just have one lock for all threads. That was the problem. Thank you.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Germán Diago
-
Ovanes Markarian
-
TONGARI