zoukankan      html  css  js  c++  java
  • pthread

    pkill 可以单独向某一个线程发信号:

    两个线程都注册了SIGUSR1信号,但只有线程tid1收到该信号

     1 #include "apue.h"
     2 #include <pthread.h>
     3 
     4 #define LOGI(format,...) printf("function: %s",format,__FUNCTION__, __VA_ARGS__)
     5 
     6 void sig_handle(int signo)
     7 {
     8     printf("%s:tid: (0x%lx) signo:%d
    ",__FUNCTION__, (unsigned long)pthread_self(),signo);
     9     pthread_exit((void*)3);
    10 }
    11 void *
    12 thr_fn1(void *arg)
    13 {
    14         signal(SIGUSR1,sig_handle);
    15         pid_t        pid;
    16     pthread_t    tid;
    17 
    18     pid = getpid();
    19     tid = pthread_self();
    20                 
    21     printf("thread 1 start
    ");
    22         printf("pid %lu tid %lu (0x%lx)
    ", (unsigned long)pid,
    23       (unsigned long)tid, (unsigned long)tid);
    24         while(1);
    25         printf("thread 1 end
    ");
    26     return((void *)1);
    27 }
    28 
    29 void sig_handle2(int signo)
    30 {
    31     printf("%s: tid: (0x%lx) signo:%d
    ", __FUNCTION__, (unsigned long)pthread_self(),signo);
    32     pthread_exit((void*)20);
    33 }
    34 
    35 void *
    36 thr_fn2(void *arg)
    37 {
    38         signal(SIGUSR1,sig_handle2);  
    39      pid_t        pid;
    40     pthread_t    tid;
    41 
    42     pid = getpid();
    43     tid = pthread_self();
    44     printf("thread 2 start
    ");
    45     printf("pid %lu tid %lu (0x%lx)
    ", (unsigned long)pid,
    46       (unsigned long)tid, (unsigned long)tid);
    47         sleep(12);
    48     printf("thread 2 end
    ");
    49     pthread_exit((void *)2);
    50 }
    51 
    52 int
    53 main(void)
    54 {
    55     int            err;
    56     pthread_t    tid1, tid2;
    57     void        *tret;
    58 
    59     err = pthread_create(&tid1, NULL, thr_fn1, NULL);
    60     if (err != 0)
    61         err_exit(err, "can't create thread 1");
    62     err = pthread_create(&tid2, NULL, thr_fn2, NULL);
    63     if (err != 0)
    64         err_exit(err, "can't create thread 2");
    65     
    66         sleep(2);
    67 
    68         pthread_kill(tid1,SIGUSR1);
    69 
    70         err = pthread_join(tid1, &tret);
    71     if (err != 0)
    72         err_exit(err, "can't join with thread 2");
    73     printf("thread 1 exit code %ld
    ", (long)tret);
    74  
    75         err = pthread_join(tid2, &tret);
    76     if (err != 0)
    77         err_exit(err, "can't join with thread 1");
    78     printf("thread 2 exit code %ld
    ", (long)tret);
    79     
    80     
    81     exit(0);
    82 }

    运行结果:

    w00367381@ubuntu:~/apue.3e/threads$ ./exitstatus 
    thread 2 start
    pid 3651 tid 140564637652736 (0x7fd7c14b9700)
    thread 1 start
    pid 3651 tid 140564646045440 (0x7fd7c1cba700)
    sig_handle:tid: (0x7fd7c1cba700) signo:10
    thread 1 exit code 3
    thread 2 end
    thread 2 exit code 2
  • 相关阅读:
    POJ 3186 Treats for the Cows
    HDU 1226 超级密码| NYOJ 929 密码宝盒
    POJ 1661 Help Jimmy
    POJ 1458 Common Subsequence
    POJ 1015 Jury Compromise
    window.history,页面中的返回按钮
    网站新功能引导镂空效果
    HTML5存储
    JSON对象
    点击超链接,将页面中某个数据传到另一个页面
  • 原文地址:https://www.cnblogs.com/hixin/p/pthread.html
Copyright © 2011-2022 走看看