2016-03-10 130 views
2

目标是在具有固定增量时间的后台线程上调用函数。C++ - 具有固定增量时间的循环POSIX线程

函数应该被调用60次/秒,因此在时间戳0,0.0166等时间戳应尽可能精确地命中。

简单但可能不是最好的解决方案是运行一段时间(true)-loop并让线程休眠直到下次调用该函数。这里有一半C++ /半伪代码它是如何做到的。

float fixedDeltaTime = 1.0/60.0; 
void loopFunction() 
{ 
     while(true) 
     { 
     auto currentTime = getTime(); 
     // do work 
     auto timePassed = getTime() - currentTime; 
     int times = (timePassed/fixedDeltaTime); 
     sleep( (fixedDeltaTime * times) - timePassed) 
     } 
} 

int main() 
{ 
    std::thread loopFunction(call_from_thread); 
    return 0; 
} 

昨天,我问了一个同样的问题,要求使用C++ 11 std :: thread的解决方案。评论中的一些人告诉我,使用POSIX线程会更好。虽然pthread对我来说似乎更加复杂,所以我希望这里的某个人能告诉我如何用pthread来解决这个问题。

+2

你有什么理由为什么你现在的解决方案不够好? –

+0

嗯,是的。 std :: sleep_for不是很准确,所以我必须在非常小的时间间隔内执行,并检查是否已调用loopFunction()的时间。在其他线程中,人们也表示计时时间测量非常昂贵。 – keyboard

+0

@keyboard - 由于我犯有提及定时器可能代价高昂的问题,因此将使用的实际增量时间是多少?如果它真的是0.0166,不要介意我对定时器性能的评论(除非你启动100个并行线程)。再一次,如果你真的想要性能,在每个平台上使用本机等待计时器,但我认为这样的超时时间太长了。 –

回答

1

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 */ 

    } 
} 

其中偏移我是您开始执行任务时等待的初始时间,而期间是您在一次调用任务和另一次调用之间等待的时间。

+1

谢谢,但似乎我不能使用这个。我得到clockid_t(和更多)未声明/找到。显然这些东西不是在iOS和OSX上:http://stackoverflow.com/questions/13472761/unknown-type-name-clockid-t – keyboard

+0

@keyboard感谢突出显示此限制。您是否尝试过在iOS上使用nanosleep()/ usleep(),如果可用()? –