[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1638
  • Last Modified:

how to schedule threads for time

Hi I'm using pthreads. I've have not been able to find a examples showing how to schedule thread execution based on time. For example if i want a thread to run every 100ms. Can anyone point me to some good example code.
  • 2
1 Solution
It's in fact impossible under linux. Linux does not guarantee You any timings.
What You need is real time scheduling - try http://www.tldp.org/HOWTO/RTLinux-HOWTO.html

Or maybe You just need to do something, then sleep for about 100ms, then do something else and so on?
You could make one of threads a scheduler, which would trigger other threads(mutex, waitqueue, etc.) at given time points...
This is typically done by having the thread's run routine be a loop that sleeps for 100ms on each iteration:

while(1) {

If your system does not support a thread-safe usleep, you could use pthread_cond_timedwait() instead by createing a private condition variable that noone will ever signal.
excuse me - You haven'k know the usleep() function?
Maybe You should then ask - how to sleep for 100ms....

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now