|
-
April 15th, 2010, 04:02 AM
#1
boost thread - shared_mutex and condition variable
Hi,
1) I would like to know the how I can put an exclusive lock on wait using a condition variable ?
Given below is my attempt, it contains 4 questions.
Code:
#include "boost/thread.hpp"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
boost :: shared_mutex m1;
boost :: condition_variable_any c1;
//Do all the 3 locks below behave like scoped_lock (will they get unlocked when they go out of scope) ?
boost :: shared_lock<boost :: shared_mutex> l1(m1); //Shared Access
boost :: upgrade_lock<boost :: shared_mutex> l2(m1); //2) Is this read lock ? no other thread can read ?
boost :: upgrade_to_unique_lock<boost :: shared_mutex> l3(l2); //Exclusive Access
c1.wait(l3); //Throws an error, how would I be able to put the l3 on wait ?
//3) Also when I do so, what happens to l2, will it also be unlocked when l3 is put on wait ??
//4) Can I create l3 without creating l2 ?
//I would like to put l3 on wait, so that what ever lock established is revoked, and when notified it can get the lock back.
//5) How would I be able to achieve this ?
return(0);
}
Last edited by Muthuveerappan; April 15th, 2010 at 04:07 AM.
-
April 15th, 2010, 07:42 PM
#2
Re: boost thread - shared_mutex and condition variable
>> c1.wait(l3);
This shouldn't compile, because "upgrade_to_unique_lock" does not implement the "Lockable Concept". It's more a utility, RAII class for going from "upgrade ownership" to "exclusive ownership" in the constructor - and then back to "upgrade ownership" in the destructor.
The current 1.42 boost docs are kinda lite on motivation behind "shared" vs "upgrade" ownership. A quick read of N2094 should help fill in some details: http://www.open-std.org/jtc1/sc22/wg...mutex_concepts
gg
-
April 16th, 2010, 01:40 AM
#3
Re: boost thread - shared_mutex and condition variable
Thanks a lot Codeplug, will read through that article and come back.
Yeah I wish boost had more detailed documentation on how and in what situations to use them.
Thanks again,
Muthu
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|