【问题标题】:Linux, C: terminate multple threads after some seconds (timer?)Linux,C:几秒钟后终止多个线程(计时器?)
【发布时间】:2015-08-18 18:20:04
【问题描述】:

Linux,C。 我创建了多个线程来运行工作负载,并且我想在指定的秒数/超时后通知这些线程停止/终止。 我如何通过 C 实现这个?

void *do_function(void *ptr)
{
    //calculating, dothe workload here;
}

int run(struct calculate_node *node)
{
    pthread_t threads[MAX_NUM_THREADS];
    for (t = 0; t < node->max_threads; t++) {
        rc = pthread_create(&threads[t], NULL, do_function, (void*)node);
        if(rc) return -1;
    }

    //how do I create timer here to fire to signal those threads to exit after specified seconds?


    for (t = 0; t < node->max_threads; t++) {
        pthread_join(threads[t], NULL);
    }
    free(threads);
}

谢谢!

【问题讨论】:

    标签: c linux multithreading timer


    【解决方案1】:

    不确定是否有一种可移植的方式来创建计时器事件,但如果main 没有其他事情可做,它可以简单地调用sleep 来浪费时间。

    至于向线程发送信号,您有两种选择:合作终止或非合作终止。对于协作终止,线程必须定期检查一个标志以查看它是否应该终止。对于非合作终止,您调用pthread_cancel 来结束线程。 (有关可用于优雅结束线程的其他功能的信息,请参阅 pthread_cancel 的手册页。)

    我发现合作终止更容易实现。这是一个例子:

    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
    
    static int QuitFlag = 0;
    static pthread_mutex_t QuitMutex = PTHREAD_MUTEX_INITIALIZER;
    
    void setQuitFlag( void )
    {
        pthread_mutex_lock( &QuitMutex );
        QuitFlag = 1;
        pthread_mutex_unlock( &QuitMutex );
    }
    
    int shouldQuit( void )
    {
        int temp;
    
        pthread_mutex_lock( &QuitMutex );
        temp = QuitFlag;
        pthread_mutex_unlock( &QuitMutex );
    
        return temp;
    }
    
    void *somefunc( void *arg )
    {
        while ( !shouldQuit() )
        {
            fprintf( stderr, "still running...\n");
            sleep( 2 );
        }
    
        fprintf( stderr, "quitting now...\n" );
        return( NULL );
    }
    
    int main( void )
    {
        pthread_t threadID;
    
        if ( pthread_create( &threadID, NULL, somefunc, NULL) != 0 )
        {
            perror( "create" );
            return 1;
        }
    
        sleep( 5 );
        setQuitFlag();
        pthread_join( threadID, NULL );
        fprintf( stderr, "end of main\n" );
    }
    

    【讨论】:

      【解决方案2】:

      首先,您需要创建并启动时间。在下面的示例中,计时器将调用“停止”函数,该函数将负责中断您的线程。在某种程度上,它是最简单的选择之一。

      void *notify_stop(void *); // Prototype
      
      int run(struct calculate_node *node)
      {
        ...
      
        struct sigevent sev;
        timer_t timer;
        struct itimerspec tspec = { 0 };
        sev.sigev_notify = SIGEV_THREAD;  // type of timer event
        sev.sigev_notify_function = notify_stop; // call function
        timer_create(CLOCK_MONOTONIC, &sevent, &timer);
        tspec.it_interval.tv_sec = 5; // set time interval
        tspec.it_interval.tv_nsec = 0;
        tspec.it_value.tv_sec = 0;
        tspec.it_value.tv_nsec = 0;
        timer_set(timer, &tspec, NULL); // start timer
      
        ....
      }
      

      现在,您需要决定如何停止线程。一种方法是使用一些共享变量来指示退出条件:

      volatile bool do_stop = false;
      void *do_function(void *ptr)
      {
        while (!do_stop)
        {
           //calculating, dothe workload here;
        }
      }
      void *notify_stop(void *)
      {
        do_stop = true; // just set the variable to let other threads
                        // read it and stop
      }
      

      【讨论】:

      • &sevent 应该是 &sev ?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 2018-02-25
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多