Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 2
    There are some assumptions in this post that I don't quite follow. Perhaps illustrating with code snippets would help? (1) Do you need one set of semaphores per class? If so, would static variables be an alternatives? Or do you want to avoid that due to initialization order issues? (2) Why would unique_ptr require you to write manual move constructors? You must follow the rule of zero/three/five, but typically you'd either follow the rule of zero and let the compiler implicitly define all necessary ctors, or = default the ctor. Commented Mar 29, 2022 at 20:59
  • @amon - Nope, each object of the class needs its own pair of semaphores, separate from other objects of that class. Commented Mar 29, 2022 at 22:06
  • What will happen if one thread locks the semaphore and does something with the object, while another thread adds some items to the vector, causing it to be reallocated, causing the object to be moved? Commented Mar 30, 2022 at 9:27
  • @user253751 - Not a concern in this case, since the code that's building the vector is single-threaded, and no client accesses happen until its completely built. However, once its built, it will get accessed multi-threaded (and yes, that means the semaphores get used by different threads than the one that created them). Commented Mar 30, 2022 at 19:28
  • @T.E.D. Then maybe you can create a "fake movable semaphore" type, and put it directly in the objects, so the CPU doesn't waste time chasing pointers. It doesn't have to actually move the semaphore, since you aren't using the semaphore when you move it, and you don't move it when you use it - it can just destroy and create a new one (while "pretending" to move) Commented Mar 30, 2022 at 20:44