首页 技术 正文
技术 2022年11月15日
0 收藏 664 点赞 3,009 浏览 2376 个字

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 线程——线程同步之死锁以及线程和信号

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,076
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894