How do i make the main thread sleep while worker threads work and worker threads sleep when waiting for main thread to execute them

This page summarizes the projects mentioned and recommended in the original post on /r/cpp_questions

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • parallel

    A c++ library to help easily spread work equally among logical processors (by PedroSilva8)

  • To avoid making this post any bigger here is a link (cpp, hpp) to an example of another way I tried to implement this using std::condition_variable and std::atomic

  • thread-pool

    BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library

  • Nope, it's definitely a problem with your code, not an external problem. This is an inevitable result when your threading scheme is not watertight. I don't have any code snippets since I'm just remembering what I've done in the past on projects, but if you can't design it to work yourself I suggest using a library. You said you've been "dealing with [multithreading] for some time now and think I have a decent understanding" but that doesn't sound like you really have a solid theoretical understanding of the subject, especially considering that it's a notoriously difficult subject. My recommendation is either to go back and plan out a watertight synchronization scheme on paper (may require finding some lecture series on multithreading theory), or to use an off-the-shelf thread pool library like this one I just found by googling thread pool library. I suggest the former if this is a hobby project, but if this code has a possibility of ever being used by other people I suggest doing the latter.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts