|
Boost Users : |
Subject: Re: [Boost-users] [Thread] Strange problem.
From: TONGARI (tongari95_at_[hidden])
Date: 2011-04-04 10:13:03
2011/4/4 Germán Diago <germandiago_at_[hidden]>
> 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_lock<boost::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.
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