Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require C++11 std::lock_guard and std::unique_lock
- Loading branch information
Showing
4 changed files
with
86 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// Copyright (c) 2016 Agustin Berge | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include <mutex> | ||
|
||
struct BasicLockable { | ||
void lock() {} | ||
void unlock() {} | ||
}; | ||
|
||
int main() | ||
{ | ||
BasicLockable basic_lockable; | ||
std::lock_guard<BasicLockable> lk(basic_lockable); | ||
|
||
std::lock_guard<BasicLockable> kl_adopt(basic_lockable, std::adopt_lock); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// Copyright (c) 2016 Agustin Berge | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include <mutex> | ||
#include <utility> | ||
|
||
struct Lockable { | ||
void lock() {} | ||
bool try_lock() { return true; } | ||
void unlock() {} | ||
}; | ||
|
||
int main() | ||
{ | ||
Lockable lockable; | ||
std::unique_lock<Lockable> lk(lockable); | ||
lk.lock(); | ||
lk.try_lock(); | ||
lk.owns_lock(); | ||
lk.unlock(); | ||
lk.release(); | ||
|
||
std::unique_lock<Lockable> mlk(std::move(lk)); | ||
|
||
std::unique_lock<Lockable> kl_adopt(lockable, std::adopt_lock); | ||
std::unique_lock<Lockable> kl_defer(lockable, std::defer_lock); | ||
std::unique_lock<Lockable> kl_try(lockable, std::try_to_lock); | ||
} |