简体   繁体   中英

make all threads sleep linux/unix

I am writing a program in linux/unix where I want to make all threads sleep, calling from one of the threads (of course including that one too). How can I do it?

sleep() only sleeps the calling process/thread, I want all of these to sleep. Thanks in advance.

The lazy solution is to fork a child process and send a STOP signal to the parent, which will suspend the parent task.

pid_t apid = fork();
if (apid == 0) { /* Child */
    kill(getppid(), SIGSTOP);
    sleep(60);
    kill(getppid(), SIGCONT);
    exit(0);
} else if (apid > 0) { /* Parent */
    int state;
    waitpid(apid, &state, 0);
} else { /* error */
    perror("fork");
}

It may not quite do what you want, but it's the lazy way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM