• 关于 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 也是有值 了。

  • 相关阅读:
    R语言(2)复杂数据类型
    Python对数据进行分类统计
    【成为红帽工程师】第四天 web服务器的实验
    边缘运算6大应用介绍
    发版检查list
    【配电变电站的最佳位置和容量】基于遗传算法的最优配电变电站放置(Matlab代码实现)
    Thymeleaf th:insert、th:replace、th:include的使用
    训练神经网络gpu占用率低,gpu为什么适合神经网络
    图论——强连通分量缩点+拓扑排序
    金仓数据库KingbaseES客户端编程接口指南-Perl DBI(6. KingbaseES Perl DBI 访问KingbaseES数据库示例)
  • 原文地址:https://blog.csdn.net/tianyexing2008/article/details/134291319