C++11 reentrant class locking strategy

后端 未结 2 1831
忘掉有多难
忘掉有多难 2021-01-12 04:05

I have an interface using the pimpl idiom, however the interface needs to be reentrant. Calling threads do not need to be aware of the locking, however. This is

2条回答
  •  被撕碎了的回忆
    2021-01-12 04:41

    Using a Pimpl idiom, the mutex should be part of the implementation. This will let you to master when the lock is started.

    BTW, why using a unique_lock when a lock_guard will be enough?

    I don't see any advantage to make impl public.

    std::unique_ptr should be as efficient as a pointer for most of the moderns compilers. Not verified however.

    I would forward the const char[N] foo_set not as

      template
      bool foo_set(const char (&new_val)[N]) { return foo_set(std::string(new_val, N)); }
    

    but like

      template
      bool foo_set(const char (&new_val)[N]) { return foo_set(N, new_val); }
    

    This avoids the string creation on the header file and let the implementation do whatever is needed.

提交回复
热议问题