Linux Threads suspend/resume

后端 未结 9 716
情歌与酒
情歌与酒 2020-12-13 11:10

I\'m writing a code in which I have two threads running in parallel.

1st is the main thread which started the 2nd thread. 2nd thread is just a simple thread executin

9条回答
  •  隐瞒了意图╮
    2020-12-13 12:12

    You can suspend a thread simply by signal

    pthread_mutex_t mutex;
    static void thread_control_handler(int n, siginfo_t* siginfo, void* sigcontext) {
        // wait time out
        pthread_mutex_lock(&mutex);
        pthread_mutex_unlock(&mutex);
    }
    // suspend a thread for some time
    void thread_suspend(int tid, int time) {
        struct sigaction act;
        struct sigaction oact;
        memset(&act, 0, sizeof(act));
        act.sa_sigaction = thread_control_handler;
        act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
        sigemptyset(&act.sa_mask);
        pthread_mutex_init(&mutex, 0);
        if (!sigaction(SIGURG, &act, &oact)) {
            pthread_mutex_lock(&mutex);
            kill(tid, SIGURG);
            sleep(time);
            pthread_mutex_unlock(&mutex);
        }
    }
    

提交回复
热议问题