Last active
May 24, 2016 07:16
-
-
Save usagi/91c9822ffa998855900c to your computer and use it in GitHub Desktop.
C++17, C++14, C++11 に併せた std::shared_mutex, std::shared_timed_mutex, std::mutex から mutex_type, read_lock_type, write_lock_type を扱う例 ref: http://qiita.com/usagi/items/4f4ee0df34688fa5c503
This file contains hidden or 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
#pragma once | |
#if __cplusplus >= 201402 | |
#include <shared_mutex> | |
#else | |
#include <mutex> | |
#endif | |
namespace usagi | |
{ | |
namespace mutex | |
{ | |
#if __cplusplus > 201402 | |
// C++17 | |
using read_write_mutex_type = std::shared_mutex; | |
using read_lock_type = std::shared_lock< read_write_mutex_type >; | |
using write_lock_type = std::lock_guard< read_write_mutex_type >; | |
#elif __cplusplus == 201402 | |
// C++14 | |
using read_write_mutex_type = std::shared_timed_mutex; | |
using read_lock_type = std::shared_lock< read_write_mutex_type >; | |
using write_lock_type = std::lock_guard< read_write_mutex_type >; | |
#elif __cplusplus >= 201103 | |
// C++11 | |
using read_write_mutex_type = std::mutex; | |
using read_lock_type = std::lock_guard< read_write_mutex_type >; | |
using write_lock_type = std::lock_guard< read_write_mutex_type >; | |
#else | |
#error not supported | |
#endif | |
} | |
} |
This file contains hidden or 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
class my_example_type | |
{ | |
my_something_type my_something; | |
usagi::mutex::read_write_mutex_type my_mutex_for_something; | |
public: | |
auto get_something_sum_of_xyz() | |
{ | |
usagi::mutex::read_lock_type read_lock( my_mutex_for_something ); | |
return my_something.x + my_something.y + my_something.z; | |
} | |
auto set_something_sum_of_xyz( const somevalue_type x, const somevalue_type y, const somevalue_type z ) | |
{ | |
usagi::mutex::write_lock_type write_lock( my_mutex_for_something ); | |
my_something.x = x; | |
my_something.y = y; | |
my_something.z = z; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment