• 关于 pthread_create 传参的疑问


            对于函数原型 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) 里的参数 arg,之前一直有疑问,就是把 &thread 传给 arg时,新创建的线程里是否能取到这个值呢?看例子:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #define handle_error_en(en, msg) \
    9. do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
    10. #define handle_error(msg) \
    11. do { perror(msg); exit(EXIT_FAILURE); } while (0)
    12. static void *thread_start(void *arg)
    13. {
    14. size_t stack_size = 0;
    15. pthread_attr_t attr;
    16. printf("in child thread id: %lu\n", *((pthread_t*)arg));
    17. int ret = pthread_getattr_np(pthread_self(), &attr);
    18. if(ret != 0)
    19. {
    20. handle_error_en(ret, "pthread_attr_init");
    21. }
    22. ret = pthread_attr_getstacksize(&attr, &stack_size);
    23. if (ret != 0)
    24. {
    25. handle_error_en(ret, "pthread_attr_setstacksize");
    26. }
    27. printf("current thread stack_size = %lu\n", stack_size);
    28. sleep(200);
    29. return 0;
    30. }
    31. void (*cancel_hook) (void *);
    32. int main(int argc, char *argv[])
    33. {
    34. int s, tnum, opt, num_threads;
    35. pthread_t thread_id;
    36. pthread_attr_t attr;
    37. int stack_size = 0x80000;
    38. void *res;
    39. printf("pid of a.out %lu\n", getpid());
    40. printf("sizeof(pthread_mutex_t) = %lu\n", sizeof(pthread_mutex_t));
    41. printf("pointer of function = %lu\n", sizeof(cancel_hook));
    42. /* Initialize thread creation attributes */
    43. s = pthread_attr_init(&attr);
    44. if (s != 0)
    45. {
    46. handle_error_en(s, "pthread_attr_init");
    47. }
    48. if (stack_size > 0)
    49. {
    50. s = pthread_attr_setstacksize(&attr, stack_size);
    51. if (s != 0)
    52. {
    53. handle_error_en(s, "pthread_attr_setstacksize");
    54. }
    55. }
    56. printf("set stack size %lu\n", stack_size);
    57. s = pthread_create(&thread_id, &attr, &thread_start, &thread_id);
    58. if (s != 0)
    59. {
    60. handle_error_en(s, "pthread_create");
    61. }
    62. printf("the child threadId %lu\n", thread_id);
    63. /* Destroy the thread attributes object, since it is no
    64. longer needed */
    65. s = pthread_attr_destroy(&attr);
    66. if (s != 0)
    67. {
    68. handle_error_en(s, "pthread_attr_destroy");
    69. }
    70. s = pthread_join(thread_id, &res);
    71. if (s != 0)
    72. {
    73. handle_error_en(s, "pthread_join");
    74. }
    75. exit(EXIT_SUCCESS);
    76. }

    新线程里打印了入参 arg 的值:printf("in child thread id: %lu\n", *((pthread_t*)arg));  因为 arg 是 void* 类型,所以需要强转一下再解引用。

    可以看到在 main 线程和子线程里打印出来的线程 id 是一样的,即在调用 pthread_create(&thread_id, &attr, &thread_start, &thread_id); 时,在新创建的线程里已经能取到 thread_id 的值。看源码片段:

    在GDB 跟踪里可以看到,在调用 create_thread 前,已经把入参 arg 赋值给了 pd->arg,经过*newthread = (pthread_t)pd,此时 arg 已经有值了,即 pd->arg 也是有值 了。

  • 相关阅读:
    工业品电商进入中场,四大阵营谁将异军突起
    好心情:双相情感障碍会影响记忆力吗
    uni-app - 城市选择索引列表 / 通过 A-Z 排序的城市列表(uview 组件库 IndexList 索引列表)
    多线程顺序运行的几种方法,面试可以随便问
    redis的原理和源码-客户端结构体的介绍和源码解析
    华为数通方向HCIP-DataCom H12-821题库(多选题:41-60)
    数据安全建设中合规管理措施
    ESP8266_接入百度物联网核心套件、使用MQTT协议通信
    Future与CompletableFuture
    linux批量杀进程命令
  • 原文地址:https://blog.csdn.net/tianyexing2008/article/details/134291319