Search code examples
c++thread-safetyfolly

standalone version of folly::Synchronized


Is there a functional equivalent of https://github.com/facebook/folly/blob/main/folly/docs/Synchronized.md that is self-contained and preferably header only so I don't have to pull in entire folly library into my project?


Solution

  • There is a dicussion on reddit where folly::Synchronized also has been mentioned and some other solutions are provided.

    Probably you're searching for something like this: https://github.com/copperspice/cs_libguarded

    A snippet of there test code:

    shared_guarded<int, shared_mutex> data(0);
    {
       auto data_handle = data.lock();
       
       ++(*data_handle);
       
       data_handle.reset();
       data_handle = data.lock();
    }
    
    auto data_handle = data.try_lock();
          
    REQUIRE(data_handle != nullptr);
    REQUIRE(*data_handle == 1);
    

    Note: cs_libguarded requires C++17.