How to avoid locking using lock_guard?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
https://en.cppreference.com/w/cpp/thread/lock_guard
(constructor)
constructs a lock_guard, optionally locking the given mutex
What would be the way to avoid locking if it is optional?
c++ multithreading locking mutex
add a comment |
https://en.cppreference.com/w/cpp/thread/lock_guard
(constructor)
constructs a lock_guard, optionally locking the given mutex
What would be the way to avoid locking if it is optional?
c++ multithreading locking mutex
3
Have you tried clicking on the(constructor)link to read more about it? It will tell you about the "optional" part and how to achieve it.
– Some programmer dude
Jan 4 at 6:26
@Someprogrammerdude No I hadn't. Thanks for pointing out.
– Aquarius_Girl
Jan 4 at 6:34
add a comment |
https://en.cppreference.com/w/cpp/thread/lock_guard
(constructor)
constructs a lock_guard, optionally locking the given mutex
What would be the way to avoid locking if it is optional?
c++ multithreading locking mutex
https://en.cppreference.com/w/cpp/thread/lock_guard
(constructor)
constructs a lock_guard, optionally locking the given mutex
What would be the way to avoid locking if it is optional?
c++ multithreading locking mutex
c++ multithreading locking mutex
asked Jan 4 at 6:16
Aquarius_GirlAquarius_Girl
7,29645151283
7,29645151283
3
Have you tried clicking on the(constructor)link to read more about it? It will tell you about the "optional" part and how to achieve it.
– Some programmer dude
Jan 4 at 6:26
@Someprogrammerdude No I hadn't. Thanks for pointing out.
– Aquarius_Girl
Jan 4 at 6:34
add a comment |
3
Have you tried clicking on the(constructor)link to read more about it? It will tell you about the "optional" part and how to achieve it.
– Some programmer dude
Jan 4 at 6:26
@Someprogrammerdude No I hadn't. Thanks for pointing out.
– Aquarius_Girl
Jan 4 at 6:34
3
3
Have you tried clicking on the
(constructor) link to read more about it? It will tell you about the "optional" part and how to achieve it.– Some programmer dude
Jan 4 at 6:26
Have you tried clicking on the
(constructor) link to read more about it? It will tell you about the "optional" part and how to achieve it.– Some programmer dude
Jan 4 at 6:26
@Someprogrammerdude No I hadn't. Thanks for pointing out.
– Aquarius_Girl
Jan 4 at 6:34
@Someprogrammerdude No I hadn't. Thanks for pointing out.
– Aquarius_Girl
Jan 4 at 6:34
add a comment |
1 Answer
1
active
oldest
votes
This is one way to avoid having the lock_guard constructor lock the given mutex :
std::mutex mtx;
mtx.lock();
std::lock_guard<std::mutex> lck(mtx, std::adopt_lock);
The intention is to allow your lock_guard to take ownership of a mutex that you already locked.
From: https://en.cppreference.com/w/cpp/thread/lock_guard/lock_guard
explicit lock_guard( mutex_type& m ); (1) (since C++11)
lock_guard( mutex_type& m, std::adopt_lock_t t ); (2) (since C++11)
lock_guard( const lock_guard& ) = delete; (3) (since C++11)
Acquires ownership of the given mutex m.
1) Effectively calls m.lock(). The behavior is undefined if m is not a recursive mutex and the current thread already owns m.
2) Acquires ownership of the mutex m without attempting to lock it.
The behavior is undefined if the current thread does not own m.
3) Copy constructor is deleted.
The behavior is undefined if m is destroyed before the lock_guard object is.
Doeslock_guard(mutex_type& m, std::adopt_lock_t t);check whethermis locked? In the case, it is not locked, doeslock_guard::~lock_guard()skip the call ofunlock()? I would expect this. However, I wonder it isn't mentioned in the cppreference doc. (An MCVE for demo would be nice.)
– Scheff
Jan 4 at 7:00
@Scheff, "The behavior is undefined if the current thread does not own m." - here, "does not own" means "have not locked". So you get undefined behavior if you pass an unlockedmutexto thelockguardconstructor.
– Sid S
Jan 4 at 7:22
May be, I focused too much on the "optional". I understood the question as "How to uselock_guardoptionally?" Reading it again I seem to have mis-understood this... (Sorry, my fault.)
– Scheff
Jan 4 at 7:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54033896%2fhow-to-avoid-locking-using-lock-guard%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is one way to avoid having the lock_guard constructor lock the given mutex :
std::mutex mtx;
mtx.lock();
std::lock_guard<std::mutex> lck(mtx, std::adopt_lock);
The intention is to allow your lock_guard to take ownership of a mutex that you already locked.
From: https://en.cppreference.com/w/cpp/thread/lock_guard/lock_guard
explicit lock_guard( mutex_type& m ); (1) (since C++11)
lock_guard( mutex_type& m, std::adopt_lock_t t ); (2) (since C++11)
lock_guard( const lock_guard& ) = delete; (3) (since C++11)
Acquires ownership of the given mutex m.
1) Effectively calls m.lock(). The behavior is undefined if m is not a recursive mutex and the current thread already owns m.
2) Acquires ownership of the mutex m without attempting to lock it.
The behavior is undefined if the current thread does not own m.
3) Copy constructor is deleted.
The behavior is undefined if m is destroyed before the lock_guard object is.
Doeslock_guard(mutex_type& m, std::adopt_lock_t t);check whethermis locked? In the case, it is not locked, doeslock_guard::~lock_guard()skip the call ofunlock()? I would expect this. However, I wonder it isn't mentioned in the cppreference doc. (An MCVE for demo would be nice.)
– Scheff
Jan 4 at 7:00
@Scheff, "The behavior is undefined if the current thread does not own m." - here, "does not own" means "have not locked". So you get undefined behavior if you pass an unlockedmutexto thelockguardconstructor.
– Sid S
Jan 4 at 7:22
May be, I focused too much on the "optional". I understood the question as "How to uselock_guardoptionally?" Reading it again I seem to have mis-understood this... (Sorry, my fault.)
– Scheff
Jan 4 at 7:47
add a comment |
This is one way to avoid having the lock_guard constructor lock the given mutex :
std::mutex mtx;
mtx.lock();
std::lock_guard<std::mutex> lck(mtx, std::adopt_lock);
The intention is to allow your lock_guard to take ownership of a mutex that you already locked.
From: https://en.cppreference.com/w/cpp/thread/lock_guard/lock_guard
explicit lock_guard( mutex_type& m ); (1) (since C++11)
lock_guard( mutex_type& m, std::adopt_lock_t t ); (2) (since C++11)
lock_guard( const lock_guard& ) = delete; (3) (since C++11)
Acquires ownership of the given mutex m.
1) Effectively calls m.lock(). The behavior is undefined if m is not a recursive mutex and the current thread already owns m.
2) Acquires ownership of the mutex m without attempting to lock it.
The behavior is undefined if the current thread does not own m.
3) Copy constructor is deleted.
The behavior is undefined if m is destroyed before the lock_guard object is.
Doeslock_guard(mutex_type& m, std::adopt_lock_t t);check whethermis locked? In the case, it is not locked, doeslock_guard::~lock_guard()skip the call ofunlock()? I would expect this. However, I wonder it isn't mentioned in the cppreference doc. (An MCVE for demo would be nice.)
– Scheff
Jan 4 at 7:00
@Scheff, "The behavior is undefined if the current thread does not own m." - here, "does not own" means "have not locked". So you get undefined behavior if you pass an unlockedmutexto thelockguardconstructor.
– Sid S
Jan 4 at 7:22
May be, I focused too much on the "optional". I understood the question as "How to uselock_guardoptionally?" Reading it again I seem to have mis-understood this... (Sorry, my fault.)
– Scheff
Jan 4 at 7:47
add a comment |
This is one way to avoid having the lock_guard constructor lock the given mutex :
std::mutex mtx;
mtx.lock();
std::lock_guard<std::mutex> lck(mtx, std::adopt_lock);
The intention is to allow your lock_guard to take ownership of a mutex that you already locked.
From: https://en.cppreference.com/w/cpp/thread/lock_guard/lock_guard
explicit lock_guard( mutex_type& m ); (1) (since C++11)
lock_guard( mutex_type& m, std::adopt_lock_t t ); (2) (since C++11)
lock_guard( const lock_guard& ) = delete; (3) (since C++11)
Acquires ownership of the given mutex m.
1) Effectively calls m.lock(). The behavior is undefined if m is not a recursive mutex and the current thread already owns m.
2) Acquires ownership of the mutex m without attempting to lock it.
The behavior is undefined if the current thread does not own m.
3) Copy constructor is deleted.
The behavior is undefined if m is destroyed before the lock_guard object is.
This is one way to avoid having the lock_guard constructor lock the given mutex :
std::mutex mtx;
mtx.lock();
std::lock_guard<std::mutex> lck(mtx, std::adopt_lock);
The intention is to allow your lock_guard to take ownership of a mutex that you already locked.
From: https://en.cppreference.com/w/cpp/thread/lock_guard/lock_guard
explicit lock_guard( mutex_type& m ); (1) (since C++11)
lock_guard( mutex_type& m, std::adopt_lock_t t ); (2) (since C++11)
lock_guard( const lock_guard& ) = delete; (3) (since C++11)
Acquires ownership of the given mutex m.
1) Effectively calls m.lock(). The behavior is undefined if m is not a recursive mutex and the current thread already owns m.
2) Acquires ownership of the mutex m without attempting to lock it.
The behavior is undefined if the current thread does not own m.
3) Copy constructor is deleted.
The behavior is undefined if m is destroyed before the lock_guard object is.
edited Jan 4 at 6:56
Scheff
8,36821426
8,36821426
answered Jan 4 at 6:29
Sid SSid S
4,35021023
4,35021023
Doeslock_guard(mutex_type& m, std::adopt_lock_t t);check whethermis locked? In the case, it is not locked, doeslock_guard::~lock_guard()skip the call ofunlock()? I would expect this. However, I wonder it isn't mentioned in the cppreference doc. (An MCVE for demo would be nice.)
– Scheff
Jan 4 at 7:00
@Scheff, "The behavior is undefined if the current thread does not own m." - here, "does not own" means "have not locked". So you get undefined behavior if you pass an unlockedmutexto thelockguardconstructor.
– Sid S
Jan 4 at 7:22
May be, I focused too much on the "optional". I understood the question as "How to uselock_guardoptionally?" Reading it again I seem to have mis-understood this... (Sorry, my fault.)
– Scheff
Jan 4 at 7:47
add a comment |
Doeslock_guard(mutex_type& m, std::adopt_lock_t t);check whethermis locked? In the case, it is not locked, doeslock_guard::~lock_guard()skip the call ofunlock()? I would expect this. However, I wonder it isn't mentioned in the cppreference doc. (An MCVE for demo would be nice.)
– Scheff
Jan 4 at 7:00
@Scheff, "The behavior is undefined if the current thread does not own m." - here, "does not own" means "have not locked". So you get undefined behavior if you pass an unlockedmutexto thelockguardconstructor.
– Sid S
Jan 4 at 7:22
May be, I focused too much on the "optional". I understood the question as "How to uselock_guardoptionally?" Reading it again I seem to have mis-understood this... (Sorry, my fault.)
– Scheff
Jan 4 at 7:47
Does
lock_guard(mutex_type& m, std::adopt_lock_t t); check whether m is locked? In the case, it is not locked, does lock_guard::~lock_guard() skip the call of unlock()? I would expect this. However, I wonder it isn't mentioned in the cppreference doc. (An MCVE for demo would be nice.)– Scheff
Jan 4 at 7:00
Does
lock_guard(mutex_type& m, std::adopt_lock_t t); check whether m is locked? In the case, it is not locked, does lock_guard::~lock_guard() skip the call of unlock()? I would expect this. However, I wonder it isn't mentioned in the cppreference doc. (An MCVE for demo would be nice.)– Scheff
Jan 4 at 7:00
@Scheff, "The behavior is undefined if the current thread does not own m." - here, "does not own" means "have not locked". So you get undefined behavior if you pass an unlocked
mutex to the lockguard constructor.– Sid S
Jan 4 at 7:22
@Scheff, "The behavior is undefined if the current thread does not own m." - here, "does not own" means "have not locked". So you get undefined behavior if you pass an unlocked
mutex to the lockguard constructor.– Sid S
Jan 4 at 7:22
May be, I focused too much on the "optional". I understood the question as "How to use
lock_guard optionally?" Reading it again I seem to have mis-understood this... (Sorry, my fault.)– Scheff
Jan 4 at 7:47
May be, I focused too much on the "optional". I understood the question as "How to use
lock_guard optionally?" Reading it again I seem to have mis-understood this... (Sorry, my fault.)– Scheff
Jan 4 at 7:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54033896%2fhow-to-avoid-locking-using-lock-guard%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Have you tried clicking on the
(constructor)link to read more about it? It will tell you about the "optional" part and how to achieve it.– Some programmer dude
Jan 4 at 6:26
@Someprogrammerdude No I hadn't. Thanks for pointing out.
– Aquarius_Girl
Jan 4 at 6:34