【问题标题】:Thread timer in cc中的线程计时器
【发布时间】:2011-01-14 19:25:12
【问题描述】:

有人可以帮我把我的代码转换成可以工作的代码吗:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

/* call back function - inform the user the time has expired */
void timeout_call_back()
{
    printf("=== your time is up ===\n");
    // doing some other stuff
}

/* Go to sleep for a period of seconds */
static void* start_timer(void *args)
{
    /* function pointer */
    void (*finish_function)();


    int seconds = *((int*) args);
    pthread_mutex_lock(&mutex); // I want to do this action atomically
    printf("thread ID : %ld, go to sleep for %d\n", pthread_self(), seconds);

    finish_function = timeout_call_back;

    // ATOMIC PART
    // This function is called in real time, so I need it
    // to be safe, and it's executed just when the timer is not reached.
    atomic_function_callback();

    // THIS IS MY PROBLEM. I WANT TO WAIT for (seconds) seconds,
    // but I don't want to block other threads.
    sleep(seconds); 

    /* call the cb to inform the user time is out */
    (*finish_function)();
    pthread_mutex_unlock(&mutex);

    pthread_exit(NULL);
}

int main()
{
    pthread_t thread1, thread2, thread3;
    int seconds1 = 300;
    int seconds2 = 600;
    int seconds3 = 900;

    int rc1, rc2, rc3;

    rc1 =  pthread_create(&thread1, NULL, start_timer, (void *) &seconds1);
    if(rc1)
    printf("=== Failed to create thread1\n");
    rc2 =  pthread_create(&thread2, NULL, start_timer, (void *) &seconds2);
    if(rc2)
    printf("=== Failed to create thread2\n");

    rc3 =  pthread_create(&thread3, NULL, start_timer, (void *) &seconds3);
    if(rc3)
    printf("=== Failed to create thread3\n");

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);

    printf("=== End of all threads in ===\n");

    return 0;
}

当我运行此代码时,一次只能运行一个线程(互斥锁的原因),但我需要这个互斥锁。是否有任何替代 sleep() 的方法可以让我做一个非阻塞计时器?

这只是一个示例,我的真实计时器非常长(8 小时)。

谢谢。

【问题讨论】:

  • 正如您的代码现在一样,没有理由在睡眠期间持有互斥锁 - 是否有充分的理由抓住该互斥锁那么长时间,即您是否可以在睡眠前不释放它之后调用并重新获取它?
  • 您是否需要通过atomic_function_callbacktimeout_call_back 来持有互斥锁?可以在这些调用之间释放互斥锁吗?

标签: c multithreading timer


【解决方案1】:

检查条件变量。 https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables

您要做的是设置条件变量并设置定时等待您的睡眠。当您调用定时等待时,将为其他线程释放互斥锁。当条件变量唤醒时,您将检查您的计时器是否已过并通知您的回调finish_function

【讨论】:

  • 如果我的代码不正常,如何创建线程计时器函数 thread_timer(pthread_t thread_id) - 因为我处于多线程环境中,所以我需要很多计时器 - 我可以检查通过从其他函数调用此计时器的结果。谢谢。
【解决方案2】:

您的问题有点含糊,但我认为您希望所有三个线程同时处于睡眠状态,但使用互斥锁确保它们中的任何一个都不会同时运行完成功能?

如果是这种情况,那么您只想使用互斥锁来保护对 finish_function 的调用,而不是保护睡眠。然后每个线程将互相休眠,当它们的时间到时,互斥锁将确保一次只有一个线程调用finish_function。

【讨论】:

  • @devariste 所以,在调用 atomic_function_callback() 后释放互斥锁,并在 sleep 调用后重新获取它。
  • 我的代码是这样的:我实时收到一个缓冲区,如果这个定时器已过期,我检查相应的定时器线程之一我运行完成功能,如果不是我做一些其他的东西。
【解决方案3】:

查看pthread_cond_timedwait

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    相关资源
    最近更新 更多