Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pthread_create in linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the use of pthread_create in linux, the content is clear, interested friends can learn, I believe it will be helpful after reading.

Pthread_create function

Brief introduction of function

Pthread_create is a thread creation function in the UNIX environment

Header file

# include

Function declaration

Int pthread_create (pthread_t * restrict tidp,const pthread_attr_t * restrict_attr,void* (* start_rtn) (void*), void* restrict 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.

Pthread_join function

Brief introduction of function

The function pthread_join is used to wait for the end of a thread.

The function prototype is:

Extern int pthread_join _ P (pthread_t _ th, void * * _ thread_return)

Parameters:

The first parameter is the waiting thread identifier

The second parameter is a user-defined pointer that can be used to store the return value of the waiting thread.

Be careful

This function is a thread blocking function, and the function that calls it will wait until the end of the waiting thread, and when the function returns, the resources of the waiting thread will be reclaimed. 0 is returned if the execution is successful, and an error number is returned if it fails.

Example:

# include#include#include/* declaration structure * / struct member {int num; char * name;}; / * define thread pthread * / static void * pthread (void * arg) {struct member * temp; / * thread pthread to start running * / printf ("pthread start!\ n"); / * keep the main thread running * / sleep (2); / * print incoming parameters * / temp = (struct member *) arg Printf ("member- > num:%d\ n", temp- > num); printf ("member- > name:%s\ n", temp- > name); return NULL;} / * main function * / int main (int agrc,char* argv []) {pthread_t tidp; struct member * b; / * assign values to structural variable b * / b = (struct member *) malloc (sizeof (struct member)); b-> num=1; b-> name= "mlq" / * create thread pthread * / if ((pthread_create (& tidp, NULL, pthread, (void*) b)) =-1) {printf ("create error!\ n"); return 1;} / * make thread pthread run * / sleep (1) first; / * thread pthread sleeps for 2s, when main can execute * / printf ("mian continue!\ n") first. / * wait for thread pthread to be released * / if (pthread_join (tidp, NULL)) {printf ("thread is not exit...\ n"); return-2;} return 0;}

Compilation and execution results

The compilation and execution results are shown in the figure below, and you can see that the main thread main and thread pthread execute alternately. That is, when we create the thread pthread, both threads are executing, proving that the creation is successful. In addition, you can see that when the thread pthread is created, the parameters passed in are printed correctly.

After reading the above content, do you have a further understanding of the use of pthread_create in linux? if you want to learn more, 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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report