C中的线程计时器 [英] Thread timer in c

查看:101
本文介绍了C中的线程计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我将我的代码转换为有效代码吗?

can someone help me to transform my code to a working code :

#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()之外,还有没有其他方法可以让我执行非阻塞计时器?

When i run this code just one thread can run in a time (cause of mutex) but i need this mutex. is there any alternative to sleep() that allow me to do a non blocking timer?

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

This is just a sample, my real timers are very long (8 hours).

谢谢.

推荐答案

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

Your question is a bit vague but I think you want all three threads to be sleeping at the same time, but use the mutex to make sure that none of them run the finish function at the same time?

如果是这种情况,那么您只想使用互斥锁来保护finish_function的调用,而不是为了保护睡眠.然后,每个线程将彼此休眠,并且随着时间的推移,互斥体将确保一次只有一个线程调用finish_function.

If that's the case then you want to use the mutex only to protect the calling of finish_function, and not to protect the sleep as well. Then each thread will sleep aside one another, and as their time is up, the mutex will ensure that only one thread calls the finish_function at a time.

这篇关于C中的线程计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆