四十四、Linux 线程——线程同步之死锁以及线程和信号

时间:2023-03-09 03:57:25
四十四、Linux 线程——线程同步之死锁以及线程和信号

44.1 死锁

  • 死锁:
    • 两个线程试图同时占有两个资源,并按不同的次序锁定相应的共享资源
  • 解决方式:
    • 按相同的次序锁定相应的共享资源
    • 使用函数 pthread_mutex_trylock(),它是函数 pthread_mutex_lock() 的非阻塞版本

44.2 线程和信号

44.2.1 介绍

  • 进程中每个线程都有自己的信号屏蔽字和信号未决字
  • 信号的处理方式是进程中所有线程共享的
  • 进程中的信号是递送到单个线程的
  • 定时器是进程资源,进程中所有的线程共享相同的定时器
    • 子线程调用 alarm() 函数产生的 alarm 信号发送给主控线程
 #include <signal.h>
int pthread_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict oset);
  • 函数功能:线程的信号屏蔽
  • 返回值:成功返回0,出错,返回错误编号

44.2.2 例子1

  子线程调用 alarm() 函数产生的 alarm 信号发送给主控线程

 #include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h> void sig_handler(int signo)
{
printf("pthread id in the sig_handler: %lx\n", pthread_self());
if(signo == SIGALRM) {
printf("timeout...\n");
}
alarm();
} void *th_fn(void *arg)
{
if(signal(SIGALRM, sig_handler) == SIG_ERR){
perror("signal sigalrm error");
} /** 在子线程中设置定时器 */
alarm(); int i;
for(i = ; i <= ; i++){
printf("(%lx) i: %d\n", pthread_self(), i);
sleep();
}
return (void *);
} int main(void)
{
int err;
pthread_t th;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if((err = pthread_create(&th, &attr, th_fn, (void *))) != ) {
perror("pthread create error");
} while(){
printf("control thread(%lx) is running\n", pthread_self());
sleep();
} printf("control thread over\n"); return ;
}

  编译运行结果如下:

  四十四、Linux 线程——线程同步之死锁以及线程和信号

44.2.3 例子 2

  线程信号屏蔽,线程发送信号给其他线程,达到一定条件后,另一线程终止另一个线程

 #include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h> void sig_handler(int signo)
{
printf("pthread id in the sig_handler: %lx\n", pthread_self());
if(signo == SIGALRM) {
printf("timeout...\n");
}
alarm();
} void *th_fn(void *arg)
{
if(signal(SIGALRM, sig_handler) == SIG_ERR){
perror("signal sigalrm error");
} /** 在子线程中设置定时器 */
alarm(); int i;
for(i = ; i <= ; i++){
printf("(%lx) i: %d\n", pthread_self(), i);
sleep();
}
return (void *);
} void *th_fn2(void *arg)
{
pthread_t th1 = (pthread_t)arg;
int i;
for(i = ; i <= ; i++){
if(i == ){
/** 终止线程 1 的运行 */
pthread_cancel(th1);
alarm();
}
printf("(%lx) i: %d\n", pthread_self(), i);
sleep();
}
return (void *);
} int main(void)
{
int err;
pthread_t th, th2;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if((err = pthread_create(&th, &attr, th_fn, (void *))) != ) {
perror("pthread create error");
} if((err = pthread_create(&th2, &attr, th_fn2, (void *)th)) != ) {
perror("pthread create error");
} sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGALRM);
/** 对主控线程屏蔽 SIGALRM 信号 */
pthread_sigmask(SIG_SETMASK, &set, NULL); while(){
printf("control thread(%lx) is running\n", pthread_self());
sleep();
} printf("control thread over\n"); return ;
}

  编译运行:

  四十四、Linux 线程——线程同步之死锁以及线程和信号