• Linux 连接已经终止的线程 线程的分离 线程取消


    连接已经终止的线程

    1. /*
    2. #include <pthread.h>
    3. int pthread_join(pthread_t thread, void **retval);
    4. -功能:连接已经终止的线程(回收子线程的资源)
    5. 阻塞函数,调用一次回收一个子线程
    6. 一般在主线程中使用
    7. -参数:
    8. -thread:需要回收的子线程的ID
    9. -retval:接受子线程退出时的返回值
    10. 返回值:
    11. -成功0
    12. -失败 非0 设置错误号
    13. */
    14. #include<stdio.h>
    15. #include<pthread.h>
    16. #include<string.h>
    17. #include<unistd.h>
    18. int value = 10;
    19. void * callback(void* arg) {
    20. printf("child thread id %ld\n", pthread_self());
    21. pthread_exit((void*) &value);
    22. }
    23. int main() {
    24. pthread_t tid;
    25. int ret = pthread_create(&tid, NULL, callback, NULL);
    26. if(ret != 0) {
    27. char * strerr = strerror(ret);
    28. printf("error:%s\n", strerr);
    29. }
    30. for(int i = 0; i < 5; i++) {
    31. printf("%d\n", i);
    32. }
    33. printf("child thread id %ld, main tid: %ld\n", tid, pthread_self());
    34. int * thread_returnvalue;
    35. ret = pthread_join(tid, (void**)&thread_returnvalue);
    36. if(ret != 0) {
    37. char* strerr = strerror(ret);
    38. printf("error: %s\n", strerr);
    39. }
    40. printf("exit data : %d\n", *thread_returnvalue);
    41. printf("child thread id %ld, main tid: %ld\n", tid, pthread_self());
    42. //主线程退出,其他线程不受影响
    43. pthread_exit(NULL);
    44. return 0;
    45. }

    线程的分离

    1. /*
    2. #include
    3. int pthread_detach(pthread_t thread);
    4. 功能:分离一个线程。被分离的线程在终止的时候会自动释放资源给系统
    5. 不能多次分离,会产生不可预料的行为。
    6. 不能去连接一个已经分离的线程,会报错
    7. 参数:需要分离的线程的ID
    8. 返回值:成功:0
    9. 失败:错误号
    10. */
    11. #include
    12. #include
    13. #include
    14. #include
    15. void* callback(void* arg) {
    16. printf("child thread id:%ld", pthread_self());
    17. return NULL;
    18. }
    19. int main() {
    20. pthread_t tid;
    21. int ret = pthread_create(&tid, NULL, callback, NULL);
    22. if(ret != 0) {
    23. char* strerr = strerror(ret);
    24. printf("error: %s\n", strerr);
    25. }
    26. printf("tid: %ld, main thread id:%ld\n", tid, pthread_self());
    27. pthread_detach(tid);
    28. ret = pthread_join(tid, NULL);
    29. if(ret != 0) {
    30. char* strerr = strerror(ret);
    31. printf("error: %s\n", strerr);
    32. }
    33. pthread_exit(NULL);
    34. return 0;
    35. }

    线程取消

    1. /*
    2. #include
    3. int pthread_cancel(pthread_t thread);
    4. 功能:取消线程(让线程终止)
    5. 取消线程,可以终止某个线程的运行
    6. 但是不是立马取消,而是当子线程执行到一个取消点,线程才会终止。
    7. 取消点:系统规定好的一些系统调用,我们可以粗略的理解为从用户区到内核区的切换的位置
    8. 参数:线程id
    9. */
    10. #include
    11. #include
    12. #include
    13. #include
    14. void* callback(void* arg) {
    15. printf("child thread id:%ld", pthread_self());
    16. for(int i = 0; i < 5; i++) {
    17. printf("%d\n", i);
    18. }
    19. return NULL;
    20. }
    21. int main() {
    22. pthread_t tid;
    23. int ret = pthread_create(&tid, NULL, callback, NULL);
    24. if(ret != 0) {
    25. char* strerr = strerror(ret);
    26. printf("error: %s\n", strerr);
    27. }
    28. pthread_cancel(tid);
    29. for(int i = 0; i < 5; i++) {
    30. printf("%d\n", i);
    31. }
    32. printf("tid: %ld, main thread id:%ld\n", tid, pthread_self());
    33. pthread_exit(NULL);
    34. return 0;
    35. }

  • 相关阅读:
    扩展卡尔曼滤波器
    redis cluster伪集群搭建及应用
    年薪30万+的HR这样做数据分析!(附关键指标&免费模版)
    Logback 相关组件
    Pytorch图像模型转ONNX后出现色偏问题
    强化学习从基础到进阶-案例与实践[2]:马尔科夫决策、贝尔曼方程、动态规划、策略价值迭代
    shiro集成 spring-加密md5配置--权限管理-shiro中的session 等等!!
    百趣土壤非靶标代谢组学文献分享,来自Microbiome的灵感
    如何下不可选中的文章
    专题·AC自动机
  • 原文地址:https://blog.csdn.net/weixin_44273624/article/details/133174013