title: Sleep or semaphore for background thread
body: <p>I have third party code which creates a lot of threads with code like this:</p>
  
  <pre><code>while (true) {
      {
          my::Lock lock(&amp;mMutex); // mutex implementation in c++
          if (!reseting) {
              // some code
              break;
          }
      }
      usleep(1000 / 20); // 20 time per second
  }
  </code></pre>
  
  <p>I can rewrite this code with <code>semaphore</code>. What to use, <code>semaphore</code> or <code>sleep</code>? As I understood they work same. <code>Semaphore</code> will work little bit faster because we can immediately continue this <code>thread</code> when <code>reset</code> is changed. </p>
  
  <p>Or maybe you have another idea how to do this better?</p>
  
  <hr>
  
  <p>Implementation of <code>my::Lock</code>:</p>
  
  <pre><code>Lock::Lock(pthread_mutex_t *mutex) {
      _mutex = mutex;
      pthread_mutex_lock(_mutex);
  }
  
  Lock::~Lock() {
      pthread_mutex_unlock(_mutex);
  }
  </code></pre>