[thread] locking and time

Hello, I have some questions about threads. It would be very nice if somebody could clarify. 1) recursive_timed_mutex: It is possible to use a time period for locking of a recursive_timed_mutex by using the approprioate timed_lock(). But there is not lock class with such a constructor? 2) (recursive timed) shared mutex: Is it not possible to lock with a period of time instead of ptime? 3) change of system time: What happens in a timed_lock if the system time is advanced by e.g. one hour? Is there a difference of using ptime or time_period? 4) read/writer mutex: if there are some recursive locks by one thread and this thread should become the writer, do I run in a deadlock? 5) What is the meaning of the various ..._lock_t (e.g. )? Is there a documentation? ad 1) There seems only to be a constructor with ABSOLUTE time: unique_lock(Lockable & m,boost::system_time const& abs_time) ad 2) There seems no possibility to use boost::posix_time::time_period for locking. ad 3) If the clock is set to one hour in the future. I think a timed_lock could take one hour at this moment? If yes, does it happen also if I give a time period? ad 4) if it is possible to recusively lock in one thread, it should be possible to make the thread a writtrer thread (exclusive owner) independently on how often it was locked before. Is this possible? ad 5) Only internal usage?: unique_lock(Lockable& m_,adopt_lock_t); unique_lock(Lockable& m_,defer_lock_t); unique_lock(Lockable& m_,try_to_lock_t) Thank you in advance, Greetings, Ernst

Hi,
----- Original Message -----
From: "Ernst Murnleitner"
Hello,
I have some questions about threads. It would be very nice if somebody could clarify.
1) recursive_timed_mutex: It is possible to use a time period for locking of a recursive_timed_mutex by using the approprioate timed_lock(). But there is not lock class with such a constructor?
You are right. The standard includes template <class Duration> unique_lock(mutex_type& m, const Duration& rel_time);
2) (recursive timed) shared mutex: Is it not possible to lock with a period of time instead of ptime? shared mutex are not recursive.
You are right. The folowing should be added template<typename TimeDuration> bool shared_mutex::timed_lock_shared(TimeDuration const & relative_time); template<typename TimeDuration> bool shared_mutex::timed_lock(TimeDuration const & relative_time);
3) change of system time: What happens in a timed_lock if the system time is advanced by e.g. one hour? Is there a difference of using ptime or time_period? I supose that the lock will expire. I don't think there is any difference because the relative time interface uses the absolute time one.
4) read/writer mutex: if there are some recursive locks by one thread and this thread should become the writer, do I run in a deadlock? There are no recursive read/writer mutex in Boost.Thread.
5) What is the meaning of the various ..._lock_t (e.g. )? Is there a documentation?
The Boost.Thread library documents this partially (http://www.boost.org/doc/libs/1_35_0/doc/html/thread/synchronization.html#th...). It document the types but not the variables. In http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html#thread.lo... you can find a more information "Some lock constructors may take tag types, which describe what should be done with the mutex during the lock's construction. struct defer_lock_t {}; // do not acquire ownership of the mutex struct try_to_lock_t {}; // try to acquire ownership of the mutex // without blocking struct adopt_lock_t {}; // assume the current thread has already // obtained mutex ownership and manage it extern const defer_lock_t defer_lock; extern const try_to_lock_t try_to_lock; extern const adopt_lock_t adopt_lock; " You use them as follows: boost::mutex mtx; boost::unique_lockboost::mutex lock(mtx, boost::defer_lock); // the mutex is not locked here or boost::mutex mtx; mtx.lock(); boost::unique_lockboost::mutex lock(mtx, boost::adopt_lock); // lock will unclock the mutex on destruction or boost::mutex mtx; boost::unique_lockboost::mutex lock(mtx, boost::try_to_lock); // here the mutex can be lock or not, you need to check with lock.owns_lock() You should do a ticket on the Trac system for each issue. Best, Vicente

Hello Vicente,
2) (recursive timed) shared mutex: Is it not possible to lock with a period of time instead of ptime?
shared mutex are not recursive.
So I had to check if I there is no place where another function which also aquires a lock is called within the locked scope. But maybe I should keep the old code. Now I use a code based on boost::threads from http://paulbridger.net/read_write_lock which I modified in order to work with timed locks.
You are right. The folowing should be added template<typename TimeDuration> bool shared_mutex::timed_lock_shared(TimeDuration const & relative_time); template<typename TimeDuration> bool shared_mutex::timed_lock(TimeDuration const & relative_time);
3) change of system time: What happens in a timed_lock if the system time is advanced by e.g. one hour? Is there a difference of using ptime or time_period?
I supose that the lock will expire. I don't think there is any difference because the relative time interface uses the absolute time one.
If I modify the time in order it is one hour earlier, I guess the lock could take one hour. This is even a bigger a problem with the sleep function of the threads library. So it is not possible to change the system time to earlier values for certain applications.
You should do a ticket on the Trac system for each issue.
Yes, I submitted them. Greetings Ernst

----- Original Message -----
From: "mur"
Hello Vicente,
2) (recursive timed) shared mutex: Is it not possible to lock with a period of time instead of ptime?
shared mutex are not recursive.
So I had to check if I there is no place where another function which also aquires a lock is called within the locked scope.
You can this at two levels: Let me say if f call g and both lock the same mutex, you can define a 'private' ts_g that has a unique_lock as parameter f() { unique_lockboost::shared_mutex lock(mtx_); // ... ts_g(lock); } g() { unique_lockboost::shared_mutex lock(mtx_); ts_g(lock); } ts_g(unique_lockboost::shared_mutex& lock) { // this pretends that a unique_lock on mtx_ has been locked. ts_g(lock); } HTH.
But maybe I should keep the old code. Now I use
a code based on boost::threads from http://paulbridger.net/read_write_lock
which I modified in order to work with timed locks.
I would prefer an implementation that follows the interface of the shared_mutex, unique_lock, shared_lock and upgradable_lock. Why not try to implement recursive_shared_mutex with the interface of shared_mutex?
You are right. The folowing should be added template<typename TimeDuration> bool shared_mutex::timed_lock_shared(TimeDuration const & relative_time); template<typename TimeDuration> bool shared_mutex::timed_lock(TimeDuration const & relative_time);
3) change of system time: What happens in a timed_lock if the system time is advanced by e.g. one hour? Is there a difference of using ptime or time_period?
I supose that the lock will expire. I don't think there is any difference because the relative time interface uses the absolute time one.
If I modify the time in order it is one hour earlier, I guess the lock could take one hour. This is even a bigger a problem with the sleep function of the threads library. So it is not possible to change the system time to earlier values for certain applications.
You are right. I have take a look on the C++0x standard and they are changing the specification of all the timed operation with duration parameters to use monotonic clocks (If I understand it a monotonic clock can not be changed in oposition to a system clock). This address directly your issue. I hope that the Boost libraries working with duration types will on its turn modify the implementation but I think that the Boost.Chrono library will be needed. Vicente
participants (3)
-
Ernst Murnleitner
-
mur
-
vicente.botet