In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what is the function of linux to create threads". The editor shows you the operation process through an actual case, the operation method is simple and fast, and it is practical. I hope this article "what is the function of linux to create threads" can help you solve the problem.
Linux has a function to create a thread, the "pthread_create ()" function. This function is the function of creating thread in Unix-like operating system, and supports four parameters: parameter 1 is the pointer to the thread identifier, parameter 2 is used to set thread properties, parameter 3 is the starting address of the thread running function, and parameter 4 is the parameter of the running function.
The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
The function of linux to create a thread is the pthread_create () function.
Pthread_create () is a function that creates threads in Unix-like operating systems (Unix, Linux, Mac OS X, etc.)
Header file
# include
Function declaration
Int pthread_create (pthread_t * restrict tidp, / / the memory unit pointed to by the newly created thread ID. Const pthread_attr_t * restrict attr, / / Thread attribute. Default is NULL void * (* start_rtn) (void *). / / the newly created thread runs void * restrict arg / / from the address of the start_rtn function. The default is NULL. The above function takes parameters, putting them into the structure and passing in the address as arg. );
Return value
Return 0 if successful, otherwise return error number
Parameters.
The first parameter is a pointer to the thread identifier.
The second parameter is used to set thread properties.
The third parameter is the address where the thread runs the function.
The last parameter is the parameter of the running function.
Be careful
Note that the-lpthread parameter is added at compile time to invoke the static link library. Because pthread is not the default library for Linux systems.
Function usage
# include # include "main.h" using namespace std;struct Sample {uint32_t index; char sex; uint32_t age; uint32_t result;}; void* TaskEntry (void* args) {Sample* sa = (Sample*) args; uint32_t num = sa- > index; if (num = = 0) {printf ("TaskEntry entry num = 0\ n"); / / Thread 1 executor sleep (10) Printf ("TaskEntry entry num = 0 is overloads!\ n");} else if (num = = 1) {printf ("TaskEntry entry num = 1\ n"); / / Thread 2 executor sleep (10); printf ("TaskEntry entry num = 1 is overloaded packages!\ n");} else if (num = = 2) {printf ("TaskEntry entry num = 2\ n") / / Thread 3 executor sleep (2); printf ("TaskEntry entry num = 2 is overloaded tasks!\ n");}} uint32_t CreateTask (pthread_t& pid, Sample& sample) {/ / assuming Sample.index = = 2 failed to create the task, return if (sample.index = = 2) {return 2;} pthread_attr_t attr directly / / set thread properties pthread_attr_init (& attr); pthread_attr_setstacksize (& attr, 64 * 1024); / / set thread stack size to 64KB uint32_t ret = pthread_create (& pid, & attr, (void* (*) (void*)) TaskEntry, (void*) & sample); if (ret! = 0) {return ret;} pthread_attr_destroy (& attr) / / cancel the setting property of thread return 0;} void VerifyTask (vector& taskID, vector& taskArgs) {void * ret; for (int index = 0; index)
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.