Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use pthread_create function

Shulou Source: shulou.com Published: 2022-06-03 11:02:31 09月11日 Update

This article mainly shows you how to use the pthread_create function. The content is simple and easy to understand. It is clearly organized. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn how to use the pthread_create function.

pthread_create is a thread-creating function for Unix-like operating systems (Unix, Linux, Mac OS X, etc.). Its function is to create a thread (in fact, determine the entry point for calling the thread function), and after the thread is created, it starts running the related thread function. Return value of pthread_create: 0 if successful; error number if error.

pthread_create function

header file

#include

function declaration

intpthread_create(pthread_t*tidp,constpthread_attr_t*attr,

void*(*start_rtn)(void*),void*arg);

compile link parameter

-lpthread

return value

If the thread is successfully created, 0 is returned. If thread creation fails, the error number is returned and the contents of *thread are undefined.

On success, the memory location pointed to by tidp is set to the thread ID of the newly created thread. The attr parameter is used to specify various thread attributes. The newly created thread runs from the address of the start_rtn function, which has only one universal pointer parameter arg. If more than one parameter needs to be passed to the start_rtn function, then these parameters need to be put into a structure, and then the address of this structure is passed as the parameter of arg.

Linux under the C language development of multi-threaded procedures, Linux system under the multi-thread follow POSIX thread interface, known as pthread.

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 the function.

The last parameter is the parameter to run the function.

pthread_create Notes

Because pthread is not the default library for Linux systems, it is the POSIX thread library. It is used as a library in Linux, so add-lpthread (or-pthread) to explicitly link the library. Error messages are returned as return values when the function executes incorrectly, without modifying the system global variable errno, and of course perror() cannot be used to print error messages.

The above is "pthread_create function how to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Thread function parameter content system run success address article error information property point pointer structure link learning help different operating system Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker Redmi Apple vpn