diff options
Diffstat (limited to 'man3/pthread_create.3')
-rw-r--r-- | man3/pthread_create.3 | 70 |
1 files changed, 35 insertions, 35 deletions
diff --git a/man3/pthread_create.3 b/man3/pthread_create.3 index f661c11ad..268bfa06b 100644 --- a/man3/pthread_create.3 +++ b/man3/pthread_create.3 @@ -4,7 +4,7 @@ .\" .\" SPDX-License-Identifier: Linux-man-pages-copyleft .\" -.TH pthread_create 3 2023-02-05 "Linux man-pages 6.03" +.TH pthread_create 3 2023-07-20 "Linux man-pages 6.05.01" .SH NAME pthread_create \- create a new thread .SH LIBRARY @@ -137,22 +137,22 @@ No permission to set the scheduling policy and parameters specified in .SH ATTRIBUTES For an explanation of the terms used in this section, see .BR attributes (7). -.ad l -.nh .TS allbox; lbx lb lb l l l. Interface Attribute Value T{ +.na +.nh .BR pthread_create () T} Thread safety MT-Safe .TE -.hy -.ad .sp 1 .SH STANDARDS -POSIX.1-2001, POSIX.1-2008. +POSIX.1-2008. +.SH HISTORY +POSIX.1-2001. .SH NOTES See .BR pthread_self (3) @@ -274,41 +274,41 @@ Joined with thread 3; returned value was SERVUS #include <stdlib.h> #include <string.h> #include <unistd.h> - +\& #define handle_error_en(en, msg) \e do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) - +\& #define handle_error(msg) \e do { perror(msg); exit(EXIT_FAILURE); } while (0) - +\& struct thread_info { /* Used as argument to thread_start() */ pthread_t thread_id; /* ID returned by pthread_create() */ int thread_num; /* Application\-defined thread # */ char *argv_string; /* From command\-line argument */ }; - +\& /* Thread start function: display address near top of our stack, and return upper\-cased copy of argv_string. */ - +\& static void * thread_start(void *arg) { struct thread_info *tinfo = arg; char *uargv; - +\& printf("Thread %d: top of stack near %p; argv_string=%s\en", tinfo\->thread_num, (void *) &tinfo, tinfo\->argv_string); - +\& uargv = strdup(tinfo\->argv_string); if (uargv == NULL) handle_error("strdup"); - +\& for (char *p = uargv; *p != \[aq]\e0\[aq]; p++) *p = toupper(*p); - +\& return uargv; } - +\& int main(int argc, char *argv[]) { @@ -318,77 +318,77 @@ main(int argc, char *argv[]) ssize_t stack_size; pthread_attr_t attr; struct thread_info *tinfo; - +\& /* The "\-s" option specifies a stack size for our threads. */ - +\& stack_size = \-1; while ((opt = getopt(argc, argv, "s:")) != \-1) { switch (opt) { case \[aq]s\[aq]: stack_size = strtoul(optarg, NULL, 0); break; - +\& default: fprintf(stderr, "Usage: %s [\-s stack\-size] arg...\en", argv[0]); exit(EXIT_FAILURE); } } - +\& num_threads = argc \- optind; - +\& /* Initialize thread creation attributes. */ - +\& s = pthread_attr_init(&attr); if (s != 0) handle_error_en(s, "pthread_attr_init"); - +\& if (stack_size > 0) { s = pthread_attr_setstacksize(&attr, stack_size); if (s != 0) handle_error_en(s, "pthread_attr_setstacksize"); } - +\& /* Allocate memory for pthread_create() arguments. */ - +\& tinfo = calloc(num_threads, sizeof(*tinfo)); if (tinfo == NULL) handle_error("calloc"); - +\& /* Create one thread for each command\-line argument. */ - +\& for (size_t tnum = 0; tnum < num_threads; tnum++) { tinfo[tnum].thread_num = tnum + 1; tinfo[tnum].argv_string = argv[optind + tnum]; - +\& /* The pthread_create() call stores the thread ID into corresponding element of tinfo[]. */ - +\& s = pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &tinfo[tnum]); if (s != 0) handle_error_en(s, "pthread_create"); } - +\& /* Destroy the thread attributes object, since it is no longer needed. */ - +\& s = pthread_attr_destroy(&attr); if (s != 0) handle_error_en(s, "pthread_attr_destroy"); - +\& /* Now join with each thread, and display its returned value. */ - +\& for (size_t tnum = 0; tnum < num_threads; tnum++) { s = pthread_join(tinfo[tnum].thread_id, &res); if (s != 0) handle_error_en(s, "pthread_join"); - +\& printf("Joined with thread %d; returned value was %s\en", tinfo[tnum].thread_num, (char *) res); free(res); /* Free memory allocated by thread */ } - +\& free(tinfo); exit(EXIT_SUCCESS); } |