In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to create threads in the Linux system. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Thread creation in the traditional Unix process model, each process has only one thread of control. In the case of a POSIX thread (pthread), when the program starts running, it also starts as a single thread of control in a single process. Before creating multiple control threads, the behavior of a program is no different from that of a traditional process. New threads can be created by calling the pthread_create function.
# include int pthread_create (pthread_t * restrict tidp, const pthread_attr_t * restrict attr, void * (* start_rtn) (void *), void * restrict arg); 12345 description: when pthread_create returns successfully, the thread ID of the newly created thread will be set to the memory unit pointed to by tidp. The attr parameter is used to customize various thread properties, which are currently set to NULL to create a thread with default properties. The newly created thread starts at the address of the start_rtn function, which has only one untyped pointer parameter, arg. If you want more than one parameter to be passed by the start_rtn function, you need to put those parameters in a structure, and then pass in the address of that structure as an arg parameter.
The example creates a thread, the print process ID, the thread ID of the new thread, and the ID of the original thread.
/ / gcc threadid.c-o a.out-pthread / / pthread is a thread library under linux. If you use multithreading, you need to link this library. Add-pthread # include "apue.h" # include # include pthread_t ntid; void printids (const char * s) {/ / declare process id pid_t pid; / / declare thread id pthread_t tid; / / get process id pid = getpid () / / get your own thread id tid = pthread_self () with pthread_self (); printf ("% s pid% lu tid% lu (0x%lx)\ n", s, (unsigned long) pid, (unsigned long) tid, (unsigned long) tid);} void * thr_fn (void * arg) {/ / call the print id function printids ("new thread:"); return (void *) 0) } int main (void) {int err; / / create a thread, the main thread stores the new thread ID in ntid, and the new thread executes the thr_fn function err = pthread_create (& ntid, NULL, thr_fn, NULL); if (err! = 0) err_exit (err, "can't create thread"); printids ("main thread:"); sleep (1); exit (0) } 123456789101112131415161718192021222324262728293031323435363738394041424344 compilation: gcc threadid.c-o a.out-pthread note:pthread is a thread library under linux, which should be linked if multithreading is used. Add-pthread to the compilation option.
Main thread: pid 1518 tid 140627397551936 (0x7fe65e13a740) new thread: pid 1518 tid 1406273891936 (0x7fe65d941700) 12 you can see that the two thread addresses are different, but the pid parent process is the same.
Another example is that the main thread accepts an input num and creates a thread to print the 2*num.
# include # include / / the return value must be void * (untyped pointer), the parameter must also be void * void * tfn (void * arg) {/ / passed in (void *), cast int num = (int) arg; long d2num = 2 numb; printf ("In thread% lu arg:% d.\ n", (unsigned long) pthread_self (), num); sleep (1) Printf ("Thread over! Arg:% d\ n ", num); return (void*) d2num;} void main (int argc, char * argv []) {pthread_t tid; long num; void* tret; / / get thread termination status while (scanf ("% ld ", & num) = = 1) {pthread_create (& tid, NULL, tfn, (void*) num) / / pthread_join (tid, & tret); printf ("Thread exit code:% ld\ n", (long) tret);} printf ("Main thread% lu is over.\ n", (unsigned long) pthread_self ()) } 123456789101112131415161718192021222325262728293031 because the code in the Unix environment high-level programming source code is using makefile links, I don't understand, so I ran the code directly into the existing file name. Here's the result: you can see the result: the created thread is different from the main thread's id number. In addition: I think in pthread_create (& tid, NULL, tfn, (void *) num); it's a bit awkward to turn num directly into a pointer, so it's like this: pthread_create (& tid, NULL, tfn, (void *) & num); then in the tfn function: int num = (int) arg; to: int num = * (int*) arg; is also possible.
This is how to create threads in the Linux system shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.