4

Linux, C. I created multiple threads to run workloads, and I want to signal those threads to stop/terminate after a specified seconds/time out. How do I implement this by 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);
}

thanks!

Howard Shane
  • 926
  • 13
  • 28

2 Answers2

4

Not sure if there's a portable way to create a timer event, but if main doesn't have anything else to do, it could simply call sleep to waste time.

As for signaling the threads, you have two choices: cooperative termination or non-cooperative termination. With cooperative termination, the thread must periodically check a flag to see if it's supposed to terminate. With non-cooperative termination, you call pthread_cancel to end the thread. (See the man page for pthread_cancel for information about additional functions that can be used to gracefully end the thread.)

I find cooperative termination easier to implement. Here's an example:

#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" );
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
2

First, you need to create and start the time. In the example below the timer will invoke a "stop" function, that will be responsible for interrupting your threads. In a way, it is one of the simplest options.

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

  ....
}

Now, you need to decide how you want to stop your threads. One way is to use some shared variable to indicate the exit condition:

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
}
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18