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

Analysis of Linux multithreaded function

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "Linux multithreaded function parsing". In daily operation, I believe many people have doubts about the parsing of Linux multithreaded functions. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "Linux multithreaded function parsing". Next, please follow the editor to study!

Analysis of Linux multithreaded function

The following three multithreaded functions are commonly used by Linux multithreaded functions

Pthread_create (), pthread_exit (), pthread_join (); they are all in the header file. Static library-lpthread needs to be added when compiling

The following is a description of the function: pthread_create is a thread creation function in the UNIX environment

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

When successful, the memory unit pointed to by tidp is set to the thread ID of the newly created thread. The attr parameter is used to define a variety of different thread properties. The newly created thread runs from the address of the start_rtn function, which has only one universal pointer parameter arg. If you need to pass more than one parameter to the start_rtn function, you need to put these parameters into a structure, and then pass in the address of that structure as an argument to arg.

C is used to develop multithreaded programs under linux, and multithreading under Linux system follows the POSIX thread interface, which is called pthread.

The pointer modified by restrict is initially the only way to access the object the pointer points to, and only if the second pointer is based on the first. Access to objects is limited to pointer expressions modified by restrict. Pointers decorated by restrict are mainly used for function parameters, or point to memory space allocated by malloc (). The restrict data type does not change the semantics of the program. The compiler can better optimize certain types of routines by making the assumption that restrict-modified pointers are the only way to access objects.

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 starting address of the thread running function.

The last parameter is the parameter of the running function.

In addition, be careful to add the-lpthread parameter at compile time to invoke the static link library. Because pthread is not the default library for Linux systems

Pthread_exit (void* retval)

A thread terminates its own execution by calling the pthread_exit function, just as the process calls the exit function at the end. The purpose of this function is to terminate the thread that called it and return a pointer to an object. The pointer can be obtained from the second parameter value_ptr in pthread_join (pthread_t tpid, void * * value_ptr).

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)

The first parameter is the identifier of the waiting thread, and the second parameter is a user-defined pointer that can be used to store the return value when the waiting thread exits. 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.

All threads have a thread number, Thread ID. Its type is pthread_t. You can get your own thread number by calling the pthread_self () function.

Here is a simple example. The child thread thread_fun will type "this is thread_fun print!" five times. Then call pthread_exit to exit and return a pointing string "this is thread return value!" The pointer. Call pthread_join in the main function to wait for the thread_ thread to finish, then read the return value of the child thread into value, and then print it out.

The output is as follows:

Pthread_create ok!

This is thread_fun print!

This is thread_fun print!

This is thread_fun print!

This is thread_fun print!

This is thread_fun print!

Pthread exit value: this is thread return value!

01.#include

02.#include

03.#include

04.#include

05.#include

06.#include

07.///

08.void * thread_fun (void * arg) {

09. Int iTunes 0

10. Char * value_ptr = "this is thread return value!\ n"

11. For (iTuno; I

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

Development

Wechat

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

12
Report