thread termination

2024-05-28 06:58
文章标签 thread termination

本文主要是介绍thread termination,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

轉載自http://stackoverflow.com/questions/6621785/posix-pthread-programming

There are mainly two approaches for thread termination.

  • Use a cancellation point. The thread will terminate when requested to cancel and it reaches a cancellation point, thus ending execution in a controlled fashion;
  • Use a signal. Have the threads install a signal handler which provides a mechanism for termination (setting a flag and reacting to EINTR).

Both approaches has caveats. Refer to kill thread in pthread for more details.

In your case, it seems a good opportunity to use cancellation points. I will work with a commented example. The error-checking has been omitted for clarity.

复制代码
 1 #define _POSIX_C_SOURCE 200809L
 2 #include <pthread.h>
 3 #include <signal.h>
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 #include <unistd.h>
 7 
 8 void sigint(int signo) {
 9     (void)signo;
10 }
11 
12 void *thread(void *argument) {
13     (void)argument;
14     for (;;) {
15         // Do something useful.
16         printf("Thread %u running.\n", *(unsigned int*)argument);
17 
18         // sleep() is a cancellation point in this example.
19         sleep(1);
20     }
21     return NULL;
22 }
23 
24 int main(void) {
25     // Block the SIGINT signal. The threads will inherit the signal mask.
26     // This will avoid them catching SIGINT instead of this thread.
27     sigset_t sigset, oldset;
28     sigemptyset(&sigset);
29     sigaddset(&sigset, SIGINT);
30     pthread_sigmask(SIG_BLOCK, &sigset, &oldset);
31 
32     // Spawn the two threads.
33     pthread_t thread1, thread2;
34     pthread_create(&thread1, NULL, thread, &(unsigned int){1});
35     pthread_create(&thread2, NULL, thread, &(unsigned int){2});
36 
37     // Install the signal handler for SIGINT.
38     struct sigaction s;
39     s.sa_handler = sigint;
40     sigemptyset(&s.sa_mask);
41     s.sa_flags = 0;
42     sigaction(SIGINT, &s, NULL);
43 
44     // Restore the old signal mask only for this thread.
45     pthread_sigmask(SIG_SETMASK, &oldset, NULL);
46 
47     // Wait for SIGINT to arrive.
48     pause();
49 
50     // Cancel both threads.
51     pthread_cancel(thread1);
52     pthread_cancel(thread2);
53 
54     // Join both threads.
55     pthread_join(thread1, NULL);
56     pthread_join(thread2, NULL);
57 
58     // Done.
59     puts("Terminated.");
60     return EXIT_SUCCESS;
61 }
复制代码

The need for blocking/unblocking signals is that if you send SIGINT to the process, any thread may be able to catch it. You do so before spawning the threads to avoid having them doing it by themselves and needing to synchronize with the parent. After the threads are created, you restore the mask and install a handler.

Cancellation points can be tricky if the threads allocates a lot of resources; in that case, you will have to use pthread_cleanup_push() and pthread_cleanup_pop(), which are a mess. But the approach is feasible and rather elegant if used properly.

这篇关于thread termination的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1009853

相关文章

Thread如何划分为Warp?

1 .Thread如何划分为Warp? https://jielahou.com/code/cuda/thread-to-warp.html  Thread Index和Thread ID之间有什么关系呢?(线程架构参考这里:CUDA C++ Programming Guide (nvidia.com)open in new window) 1维的Thread Index,其Thread

RT-Thread(Nano版本)的快速移植(基于NUCLEO-F446RE)

目录 概述 1 RT-Thread 1.1 RT-Thread的版本  1.2 认识Nano版本 2 STM32F446U上移植RT-Thread  2.1 STM32Cube创建工程 2.2 移植RT-Thread 2.2.1 安装RT-Thread Packet  2.2.2 加载RT-Thread 2.2.3 匹配相关接口 2.2.3.1 初次编译代码  2.2.3.

GTK中创建线程函数g_thread_new和g_thread_create的区别

使用GThread函数,需要引用glib.h头文件。 这两个接口的核心区别就是  g_thread_create 是旧的接口,现在已经不使用了,而g_thread_new是新的接口,建议使用。 g_thread_create: g_thread_create has been deprecated since version 2.32 and should not be used in n

基于 rt-thread的I2C操作EEPROM(AT24C02)

一、AT24C02 The AT24C01A/02/04/08A/16A provides 1024/2048/4096/8192/16384 bits of serial electrically erasable and programmable read-only memory (EEPROM) organized as 128/256/512/1024/2048 words of 8 b

[项目][CMP][Thread Cache]详细讲解

目录 1.设计&结构2.申请内存3.释放内存4.框架 1.设计&结构 Thread Cache是哈希桶结构,每个桶是一个按桶位置映射大小的内存块对象的自由链表 每个线程都会有一个Thread Cache对象,这样每个线程在这里获取对象和释放对象时是无锁的 TLS – Thread Local Strorage Linux gcc下TLSWindows vs下TLS

线程池工具类——Thread学习笔记

记录一下线程池工具类: /*** 线程池工具类* @author lixiang* @date 2018年10月10日 - 11:10* @history 2018年10月10日 - 11:10 lixiang create.*/public class ThreadPoolHelper {private static final Logger logger = LoggerFactory.g

模拟线程死锁——Thread学习笔记

记录一下之前写过的一段模拟死锁的代码: /*** 模拟死锁** @author lixiang* @date 2018年10月12日 - 9:51* @history 2018年10月12日 - 9:51 lixiang create.*/public class HoldLockDemo {private static Object[] lock = new Object[10];priv

Java多线程编程模式实战指南:Two-phase Termination模式

文章来源: http://www.infoq.com/cn/articles/java-multithreaded-programming-mode-two-phase-termination?utm_source=infoq&utm_campaign=user_page&utm_medium=link 文章代码地址: https://github.com/Visce

C++11 Thread线程池、死锁、并发

一、线程与进程         进程:运行中的程序         线程:进程中的小进程 二、线程库的使用         包含头文件#include<thread> 2.1 thread函数         具体代码: void show(string str) {cout << "This is my word : " << str << endl;}int main() {t

Exception in thread main java.lang.NoClassDefFoundError: org/apache/juli/l

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/juli/l MyEclipse整合免安装版的Tomcat7,------> 看图吧 最后这个就可以在myeclipse里,使用你的tomcat,而不是用.bat打开!!!!