简体   繁体   中英

C++ timer with only pthreads

Is there any way to write a timer in pure C/C++ with only using pthreads neither boost nor Qt. I want OS to run only the function in the relevant thread(eg MyThreadClass.myfunction()). I don't want to check the signal in every thread(there must be 1 type of thread cheking time). The timer thread must run (by interrupting other processes and threads) at the time as soon as possible and be able to calculate the time elapsed at it is not running. And it must not use while(true) like loops I don't want to make CPU busy.

For example I set my thread to run in 100ms. It goes background and checks time. It should run frequent but short. At 100ms it will interrupt threads and runs a particular function.

You'll have to be more specific about why you need to use only pthreads. The following uses librt which is part of the included system libraries on most POSIX systems.

Sounds like you want to use timer_create with the SIGEV_THREAD argument. That will create a thread and run a specified function when the timer expires.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM