zoukankan      html  css  js  c++  java
  • One Way to Suspend Another Thread In Linux

    // at process start call init_pthread_suspending to install the handlers
    // to suspend a thread use pthread_kill(thread_id, SUSPEND_SIG)
    // to resume a thread use pthread_kill(thread_id, RESUME_SIG)

    #include <signal.h>

    #define RESUME_SIG SIGUSR2
    #define SUSPEND_SIG SIGUSR1

    static sigset_t wait_mask;
    static __thread int suspended; // per-thread flag

    void resume_handler(int sig)
    {
        suspended = 0;
    }

    void suspend_handler(int sig)
    {
        if (suspended) return;
        suspended = 1;
        do sigsuspend(&wait_mask); while (suspended);
    }

    void init_pthread_suspending()
    {
        struct sigaction sa;

        sigfillset(&wait_mask);
        sigdelset(&wait_mask, SUSPEND_SIG)
        sigdelset(&wait_mask, RESUME_SIG);

        sigfillset(&sa.sa_mask);
        sa.sa_flags = 0;
        sa.sa_handler = resume_handler;
        sigaction(RESUME_SIG, &sa, NULL);

        sa.sa_handler = suspend_handler;
        sigaction(SUSPEND_SIG, &sa, NULL);
    }
  • 相关阅读:
    java Metaspace频繁FGC问题定位(转载)
    JVM内存模型详解(转载)
    56. Merge Intervals
    begin again
    55. Jump Game
    54. Spiral Matrix
    53. Maximum Subarray
    52. N-Queens II
    51. N-Queens
    1. 赋值运算符函数
  • 原文地址:https://www.cnblogs.com/len3d/p/2346235.html
Copyright © 2011-2022 走看看