Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何安排c函数精确地每分钟运行一次_C_Pthreads_Scheduled Tasks - Fatal编程技术网

如何安排c函数精确地每分钟运行一次

如何安排c函数精确地每分钟运行一次,c,pthreads,scheduled-tasks,C,Pthreads,Scheduled Tasks,我有一个使用pthread创建的线程,应该每分钟运行一次。我怎么能在c程序中每分钟运行一次呢?睡眠并不能解决我的问题。我假设(a)你的意思是睡眠不好,因为如果你在七秒钟的工作之后睡60秒,那就不是每分钟了 因此,为了让每一分钟都能完成,不管作业需要多长时间,您可以使用(伪代码): 这当然有一个缺点,如果您的工作需要一分钟以上,那么下一次迭代将被延迟。但是,除了必须处理多个并发作业之外,这可能是最好的 (a) 对我来说,这似乎是最有可能的可能性,但如果您包含了代码和/或添加了关于问题原因的详细信

我有一个使用pthread创建的线程,应该每分钟运行一次。我怎么能在c程序中每分钟运行一次呢?睡眠并不能解决我的问题。

我假设(a)你的意思是睡眠不好,因为如果你在七秒钟的工作之后睡60秒,那就不是每分钟了

因此,为了让每一分钟都能完成,不管作业需要多长时间,您可以使用(伪代码):

这当然有一个缺点,如果您的工作需要一分钟以上,那么下一次迭代将被延迟。但是,除了必须处理多个并发作业之外,这可能是最好的


(a) 对我来说,这似乎是最有可能的可能性,但如果您包含了代码和/或添加了关于问题原因的详细信息,我可能不需要这样假设:-)

作为另一种可能性,为了确保它只在
hh:mm:00
时运行(即,恰好在分钟切换时),您可以做一些轻微的更改:

def threadFn():
    lastTime = now() - 1 # Ensure run at first hh:mm:00.
    do forever:
        currTime = now()
        currSec = getSecond(currTime) # using C's localtime()
        if currSec == 0 and currTime != lastTime:
            lastTime = currTime
            doPayload()
        sleep one tenth of a second

减少睡眠是为了确保在进入新的一分钟后尽快运行有效负载。

代码在哪里?可能重复读取,然后再考虑一下。线程正在尝试将一些数据发布到重新服务器。。因此,该函数可能存在延迟。。不管怎样,我需要在下一分钟时钟的秒针转为00时准确地发布数据。
def threadFn():
    lastTime = now() - 1 # Ensure run at first hh:mm:00.
    do forever:
        currTime = now()
        currSec = getSecond(currTime) # using C's localtime()
        if currSec == 0 and currTime != lastTime:
            lastTime = currTime
            doPayload()
        sleep one tenth of a second