|
Boost Users : |
Subject: Re: [Boost-users] thread mutex problem
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2010-09-06 17:49:45
Kraus Philipp <philipp.kraus_at_[hidden]> writes:
> I'm new with boost thread, I have some problems.
> I have got two methods, one for writing and one for reading my data and I would
> do this thread-safe.
> I have tried to do this with boost::unique_lock<boost::shared_mutex> lock(...),
> but I have created a deadlock.
>
> For example my methods are called within the threads and set / get data to /
> from a ublas matrix.
> How can I do both methods in the correct way thread-safe?
boost::mutex m;
void setData( const size_t row, const size_t col, const size_t data) {
boost::unique_lock<boost::mutex> lk(m);
_matrix(row, col) = data
}
void getData( const size_t row, const size_t col, size_t& a, size_t& b) {
boost::unique_lock<boost::mutex> lk(m);
a = _matrix(row, col);
b = _matrix(col, row);
}
Once you've got the basics working, you can change the mutex to a
boost::shared_mutex and the lock in getData to a shared_lock rather than
a unique_lock if profiling determines that this is the bottle neck, *and*
the shared_mutex helps.
Anthony
-- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
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