1

I have found the answer from HowardHinnant in this thread. C++11 equivalent to boost shared_mutex

Unfortunately it seems to make use of the following macro which the Microsoft compiler (VS2015 x64) does not seem to support and the code needs to be C++11 and cross-platform.

_LIBCPP_BEGIN_NAMESPACE_STD
_LIBCPP_END_NAMESPACE_STD

I have tried to change it to just

namespace std{
...
}

Then the compiler complains with the following message.

Severity Code Description Project File Line Column Source Suppression State Tool Error C2244 'std::unique_lock<_Mutex>::unique_lock': unable to match function definition to an existing declaration shared_mutex_test c:\documents\visual studio 2015\projects\shared_mutex_test\shared_mutex.hpp 1217 1 Build

Question, how to solve this. I need shared_mutex / shared_lock functionality and I have to be C++11 compliant. We have decided not to bring in a bigger library like Boost, hence if I could get HowardHinnant's implementation to work cross-platform it would be the optimal solution.

Many thanks.

Ps. I have not yet got a reputation high enough to comment on closed questions, hence I was forced to open up this new thread. Feel free to move it into the other thread.

Johan
  • 502
  • 4
  • 18
  • 2
    Just don't put it into the std namespace - it's not a good idea anyway – UKMonkey Dec 06 '17 at 13:35
  • So I should just use another namespace? I tried that, then I got also a huge amount of errors. Like this Severity Code Description Project File Line Column Source Suppression State Tool Error C2923 'ting::shared_lock': 'mutex_type' is not a valid template type argument for parameter 'Mutex' shared_mutex_test c:\documents\visual studio 2015\projects\shared_mutex_test\shared_mutex.hpp 1203 1 Build – Johan Dec 06 '17 at 13:38
  • Same errors if I completely remove the namespace – Johan Dec 06 '17 at 13:40
  • When running clang, `_LIBCPP_BEGIN_NAMESPACE_STD` is evaluated as `namespace std {inline namespace __1 {`, you could try with this. – Holt Dec 06 '17 at 13:43
  • 1
    Do you have to use the same solution for *all* compilers? Because VS2015 of course already has a ``. – Bo Persson Dec 06 '17 at 13:44
  • @UKMonkey not a good idea, and UB ;) – YSC Dec 06 '17 at 13:44
  • @Holt thanks but this still gives error. Severity Code Description Project File Line Column Source Suppression State Tool Error C2244 'std::unique_lock<_Mutex>::unique_lock': unable to match function definition to an existing declaration shared_mutex_test c:\documents\visual studio 2015\projects\shared_mutex_test\shared_mutex.hpp 1219 1 Build – Johan Dec 06 '17 at 13:47
  • @BoPersson, yes I need to use the same solution, the goal with the framework is that it should be completely cross-platform. – Johan Dec 06 '17 at 13:48
  • @JohanHallenberg You can always include `` on VS2015 and Howard's implementation for other compilers, as long as the interface are the same. – Holt Dec 06 '17 at 13:49
  • @Holt, but we need to be C++11, I assume shared_mutex on VS2015 is not C++11 compliant. Not convinced the interfaces will be exactly the same either I am afraid. In fact we use CMake as a build system and I just let Cmake generate a VS solution for convenience as I like working in that IDE. – Johan Dec 06 '17 at 13:51
  • I get the "unable to match function definition to an existing declaration for" on this code. template template inline unique_lock::unique_lock(ting::shared_lock&& sl, const chrono::duration<_Rep, _Period>& rel_time) : __m_(nullptr), __owns_(false) { if (sl.owns_lock()) { if (sl.mutex()->try_unlock_shared_and_lock_for(rel_time)) { __m_ = sl.release(); __owns_ = true; } } else __m_ = sl.release(); } – Johan Dec 06 '17 at 13:58

1 Answers1

0

Solved by just removing all code that failed. It seems it interfered with the available c++11 implementation of unique_lock. Since I can use C++11 I did not need that part of the code.

To fully solve it the complete unique_lock class probably needs to be implemented.

Johan
  • 502
  • 4
  • 18