title: How do I make a Posix thread finish its job after a certain amount of time?
body: <p>I want to make a Posix thread finish its job after a certain amount of time has passed. You can see my solution in the simple C + Python pseudocode. But I don't think that is an efficient and accurate solution. What is the best way to achive this?</p>
  
  <pre><code>Mutex incrementLock
  BigInteger n = 0
  int milliToWork = 5000
  
  Worker()
      int elapsedMilli = 0
      while elapsedMilli &lt; milliToWork
          clock_t startClock = clock()
          Lock(incrementLock)
          n += 1
          Unlock(incrementLock)
          clock_t endClock = clock()
          elapsedMilli += (double)(endClock - startClock) / (double)CLOCKS_PER_SEC * 1000.0
  
  main()
      int nThreads = 100
      Thread threads[nThreads]
      for i = 1 to nThreads
          ThreadCreate(threads[i], Worker)
      for i = 1 to nThreads
          ThreadJoin(threads[i])
  </code></pre>