Search code examples
linuxunixsleepthread-sleep

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.


Solution

  • 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.