• pthread_create


    PTHREAD_CREATE(3)                                                                    Linux Programmer's Manual                                                                   PTHREAD_CREATE(3)

    NAME
           pthread_create - create a new thread

    SYNOPSIS
           #include

           int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                              void *(*start_routine) (void *), void *arg);

           Compile and link with -pthread.

    DESCRIPTION
           The  pthread_create()  function  starts a new thread in the calling process.  The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_rou‐
           tine().

           The new thread terminates in one of the following ways:

           * It calls pthread_exit(3), specifying an exit status value that is available to another thread in the same process that calls pthread_join(3).

           * It returns from start_routine().  This is equivalent to calling pthread_exit(3) with the value supplied in the return statement.

           * It is canceled (see pthread_cancel(3)).

           * Any of the threads in the process calls exit(3), or the main thread performs a return from main().  This causes the termination of all threads in the process.

           The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time to determine attributes for the new thread;  this  structure  is  initialized  using
           pthread_attr_init(3) and related functions.  If attr is NULL, then the thread is created with default attributes.

           Before  returning,  a  successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread; this identifier is used to refer to the thread in subsequent
           calls to other pthreads functions.

           The new thread inherits a copy of the creating thread's signal mask (pthread_sigmask(3)).  The set of pending signals for the new thread is empty (sigpending(2)).  The new thread does not
           inherit the creating thread's alternate signal stack (sigaltstack(2)).

           The new thread inherits the calling thread's floating-point environment (fenv(3)).

           The initial value of the new thread's CPU-time clock is 0 (see pthread_getcpuclockid(3)).

    Linux-specific details
           The new thread inherits copies of the calling thread's capability sets (see capabilities(7)) and CPU affinity mask (see sched_setaffinity(2)).

    RETURN VALUE
           On success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined.

    ERRORS
           EAGAIN Insufficient resources to create another thread.

           EAGAIN A  system-imposed  limit  on  the  number of threads was encountered.  There are a number of limits that may trigger this error: the RLIMIT_NPROC soft resource limit (set via setr‐
                  limit(2)), which limits the number of processes and threads for a real user ID, was reached; the kernel's system-wide limit on the number of processes and  threads,  /proc/sys/ker‐
                  nel/threads-max, was reached (see proc(5)); or the maximum number of PIDs, /proc/sys/kernel/pid_max, was reached (see proc(5)).

           EINVAL Invalid settings in attr.

           EPERM  No permission to set the scheduling policy and parameters specified in attr.

    ATTRIBUTES
           For an explanation of the terms used in this section, see attributes(7).

           ┌─────────────────┬───────────────┬─────────┐
           │Interface        │ Attribute     │ Value   │
           ├─────────────────┼───────────────┼─────────┤
           │pthread_create() │ Thread safety │ MT-Safe │
           └─────────────────┴───────────────┴─────────┘

    CONFORMING TO
           POSIX.1-2001, POSIX.1-2008.

    NOTES
           See  pthread_self(3)  for  further  information  on  the  thread  ID  returned  in  *thread  by pthread_create().  Unless real-time scheduling policies are being employed, after a call to
           pthread_create(), it is indeterminate which thread—the caller or the new thread—will next execute.

           A thread may either be joinable or detached.  If a thread is joinable, then another thread can call pthread_join(3) to wait for the thread to terminate and fetch its  exit  status.   Only
           when  a  terminated joinable thread has been joined are the last of its resources released back to the system.  When a detached thread terminates, its resources are automatically released
           back to the system: it is not possible to join with the thread in order to obtain its exit status.  Making a thread detached is useful for some types of daemon threads whose  exit  status
           the  application  does  not  need  to  care  about.   By  default,  a  new  thread  is  created  in  a  joinable state, unless attr was set to create the thread in a detached state (using
           pthread_attr_setdetachstate(3)).

           Under the NPTL threading implementation, if the RLIMIT_STACK soft resource limit at the time the program started has any value other than "unlimited", then it determines the default stack
           size  of  new  threads.   Using  pthread_attr_setstacksize(3), the stack size attribute can be explicitly set in the attr argument used to create a thread, in order to obtain a stack size
           other than the default.  If the RLIMIT_STACK resource limit is set to "unlimited", a per-architecture value is used for the stack size.  Here is the value for a few architectures:

                  ┌─────────────┬────────────────────┐
                  │Architecture │ Default stack size │
                  ├─────────────┼────────────────────┤
                  │i386         │               2 MB │
                  ├─────────────┼────────────────────┤
                  │IA-64        │              32 MB │
                  ├─────────────┼────────────────────┤
                  │PowerPC      │               4 MB │
                  ├─────────────┼────────────────────┤
                  │S/390        │               2 MB │
                  ├─────────────┼────────────────────┤
                  │Sparc-32     │               2 MB │
                  ├─────────────┼────────────────────┤
                  │Sparc-64     │               4 MB │
                  ├─────────────┼────────────────────┤
                  │x86_64       │               2 MB │
                  └─────────────┴────────────────────┘
    BUGS
           In the obsolete LinuxThreads implementation, each of the threads in a process has a different process ID.  This is in violation of the POSIX threads specification, and is  the  source  of
           many other nonconformances to the standard; see pthreads(7).

    EXAMPLE
           The program below demonstrates the use of pthread_create(), as well as a number of other functions in the pthreads API.

           In the following run, on a system providing the NPTL threading implementation, the stack size defaults to the value given by the "stack size" resource limit:

               $ ulimit -s
               8192            # The stack size limit is 8 MB (0x800000 bytes)
               $ ./a.out hola salut servus
               Thread 1: top of stack near 0xb7dd03b8; argv_string=hola
               Thread 2: top of stack near 0xb75cf3b8; argv_string=salut
               Thread 3: top of stack near 0xb6dce3b8; argv_string=servus
               Joined with thread 1; returned value was HOLA
               Joined with thread 2; returned value was SALUT
               Joined with thread 3; returned value was SERVUS

           In the next run, the program explicitly sets a stack size of 1 MB (using pthread_attr_setstacksize(3)) for the created threads:

               $ ./a.out -s 0x100000 hola salut servus
               Thread 1: top of stack near 0xb7d723b8; argv_string=hola
               Thread 2: top of stack near 0xb7c713b8; argv_string=salut
               Thread 3: top of stack near 0xb7b703b8; argv_string=servus
               Joined with thread 1; returned value was HOLA
               Joined with thread 2; returned value was SALUT
               Joined with thread 3; returned value was SERVUS

    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. struct thread_info { /* Used as argument to thread_start() */
    13. pthread_t thread_id; /* ID returned by pthread_create() */
    14. int thread_num; /* Application-defined thread # */
    15. char *argv_string; /* From command-line argument */
    16. };
    17. /* Thread start function: display address near top of our stack,
    18. and return upper-cased copy of argv_string */
    19. static void *
    20. thread_start(void *arg)
    21. {
    22. struct thread_info *tinfo = arg;
    23. char *uargv, *p;
    24. printf("Thread %d: top of stack near %p; argv_string=%s\n",
    25. tinfo->thread_num, &p, tinfo->argv_string);
    26. uargv = strdup(tinfo->argv_string);
    27. if (uargv == NULL)
    28. handle_error("strdup");
    29. for (p = uargv; *p != '\0'; p++)
    30. *p = toupper(*p);
    31. return uargv;
    32. }
    33. int
    34. main(int argc, char *argv[])
    35. {
    36. int s, tnum, opt, num_threads;
    37. struct thread_info *tinfo;
    38. pthread_attr_t attr;
    39. int stack_size;
    40. void *res;
    41. /* The "-s" option specifies a stack size for our threads */
    42. stack_size = -1;
    43. while ((opt = getopt(argc, argv, "s:")) != -1) {
    44. switch (opt) {
    45. case 's':
    46. stack_size = strtoul(optarg, NULL, 0);
    47. break;
    48. default:
    49. fprintf(stderr, "Usage: %s [-s stack-size] arg...\n",
    50. argv[0]);
    51. exit(EXIT_FAILURE);
    52. }
    53. }
    54. num_threads = argc - optind;
    55. /* Initialize thread creation attributes */
    56. s = pthread_attr_init(&attr);
    57. if (s != 0)
    58. handle_error_en(s, "pthread_attr_init");
    59. if (stack_size > 0) {
    60. s = pthread_attr_setstacksize(&attr, stack_size);
    61. if (s != 0)
    62. handle_error_en(s, "pthread_attr_setstacksize");
    63. }
    64. /* Allocate memory for pthread_create() arguments */
    65. tinfo = calloc(num_threads, sizeof(struct thread_info));
    66. if (tinfo == NULL)
    67. handle_error("calloc");
    68. /* Create one thread for each command-line argument */
    69. for (tnum = 0; tnum < num_threads; tnum++) {
    70. tinfo[tnum].thread_num = tnum + 1;
    71. tinfo[tnum].argv_string = argv[optind + tnum];
    72. /* The pthread_create() call stores the thread ID into
    73. corresponding element of tinfo[] */
    74. s = pthread_create(&tinfo[tnum].thread_id, &attr,
    75. &thread_start, &tinfo[tnum]);
    76. if (s != 0)
    77. handle_error_en(s, "pthread_create");
    78. }
    79. /* Destroy the thread attributes object, since it is no
    80. longer needed */
    81. s = pthread_attr_destroy(&attr);
    82. if (s != 0)
    83. handle_error_en(s, "pthread_attr_destroy");
    84. /* Now join with each thread, and display its returned value */
    85. for (tnum = 0; tnum < num_threads; tnum++) {
    86. s = pthread_join(tinfo[tnum].thread_id, &res);
    87. if (s != 0)
    88. handle_error_en(s, "pthread_join");
    89. printf("Joined with thread %d; returned value was %s\n",
    90. tinfo[tnum].thread_num, (char *) res);
    91. free(res); /* Free memory allocated by thread */
    92. }
    93. free(tinfo);
    94. exit(EXIT_SUCCESS);
    95. }

  • 相关阅读:
    Retrofit2 源码分析
    upp(统一流程平台)项目,如果需要项目章程会怎么写
    系统架构设计:4 论微服务架构及其应用
    C++头文件 库函数 以及 vector相关问题
    MongoDB学习(一)
    hadoop103 Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)
    CentOS7安装Nginx+ModSecurity
    [计算机毕业设计成品]精品社区心理健康服务平台小程序系统|前后分离VUE[包运行成功]
    IvorySQL3.0:基于PG16.0最新内核,实现兼容Oracle数据库再升级
    hadoop historyserver启动,无法访问,查看报错等问题
  • 原文地址:https://blog.csdn.net/fuhanga123/article/details/134281022