Posix线程肯定授予之间前台和后台任务更快的通信速度,但如果你想精密我会建议使用clock_nanosleep跟踪的时间在一个实时内核之上。
您可以使用以下基本功能来塑造你的任务生命周期:
#ifndef __TIMERS_H__
#define __TIMERS_H__
#include <stdint.h> /* uint64_t */
#include <time.h> /* clockid_t of clock_nanosleep() */
/*--------------------------------------------------------------------------------------*/
/* Common facility functions needed for
* high precision timers usage
*/
/*--------------------------------------------------------------------------------------*/
#define SEC_VAL 1000000000ULL
enum return_values {
RETURN_FAILURE = 0,
RETURN_SUCCESS = 1,
RETURN_EMPTY = 2
};
typedef void* timespec_ptr;
typedef struct timespec timespec_t;
/* Adds time_us microseconds to timer ts
*/
void timespec_add_ns(timespec_ptr ts,
uint64_t time_ns);
/* Makes the thread wait for the next activation of the timer ts
*/
void wait_next_activation(timespec_ptr ts); /*** @ Tasks ***/
/* Starts the periodic timer
*/
int start_periodic_timer(timespec_ptr ts,
uint64_t init_offs_ns);
/* Computes the difference among two clocks
*/
long calcdiff(struct timespec t1,
struct timespec t2);
/*--------------------------------------------------------------------------------------*/
extern int clock_nanosleep(clockid_t clock_id, int flags,
const struct timespec* request,
struct timespec* remain);
/*--------------------------------------------------------------------------------------*/
#endif
这里有实现文件:
#include "timers.h"
#include <stdio.h> /* fprintf */
/*--------------------------------------------------------------------------------------*/
/* Adds time_us microseconds to timer ts
*/
void timespec_add_ns(timespec_ptr ts,
uint64_t time_ns)
{
if (ts)
{
timespec_t* ts_ = (timespec_t*) ts;
time_ns += ts_->tv_nsec;
ts_->tv_sec += time_ns/SEC_VAL;
ts_->tv_nsec = time_ns%SEC_VAL;
} else {
fprintf(stderr, "Warning (%s): input argument is NULL, \
request ignored.\n", __FUNCTION__);
}
}
/* Makes the thread wait for the next activation of the timer ts
*/
void wait_next_activation(timespec_ptr ts)
{
if (ts)
{
timespec_t* ts_ = (timespec_t*) ts;
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, ts_, NULL);
} else {
fprintf(stderr, "Warning (%s): input parameter is NULL, \
request ignored.\n", __FUNCTION__);
}
}
/* Starts the periodic timer
*/
int start_periodic_timer(timespec_ptr ts,
uint64_t init_offs_ns) /*** @ Tasks ***/
{
if (ts)
{
timespec_t* ts_ = (timespec_t*) ts;
clock_gettime(CLOCK_MONOTONIC, ts_);
timespec_add_ns(ts, init_offs_ns);
return RETURN_SUCCESS;
} else {
fprintf(stderr, "Warning (%s): input parameter is NULL, \
request ignored.\n", __FUNCTION__);
return RETURN_FAILURE;
}
}
/* Computes the difference among two clocks
*/
long calcdiff(struct timespec t1,
struct timespec t2) /*** @ Tasks ***/
{
long diff;
diff = SEC_VAL * ((int) t1.tv_sec - (int) t2.tv_sec);
diff += ((int) t1.tv_nsec - (int) t2.tv_nsec);
return diff;
}
/*--------------------------------------------------------------------------------------*/
后台任务应主要做到以下几点:
void run (void *args) {
start_periodict_timer(&timer_, offset);
while (true) {
wait_next_activation(&timer_);
timespec_add_ns(&timer_, period);
/* do your periodic task */
}
}
其中偏移我是您开始执行任务时等待的初始时间,而期间是您在一次调用任务和另一次调用之间等待的时间。
你有什么理由为什么你现在的解决方案不够好? –
嗯,是的。 std :: sleep_for不是很准确,所以我必须在非常小的时间间隔内执行,并检查是否已调用loopFunction()的时间。在其他线程中,人们也表示计时时间测量非常昂贵。 – keyboard
@keyboard - 由于我犯有提及定时器可能代价高昂的问题,因此将使用的实际增量时间是多少?如果它真的是0.0166,不要介意我对定时器性能的评论(除非你启动100个并行线程)。再一次,如果你真的想要性能,在每个平台上使用本机等待计时器,但我认为这样的超时时间太长了。 –