In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the use of the multithreaded pthread_create () function. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the usage of the multithreaded pthread_create () function.
Summary: pthread_create is a function for creating threads in operating systems such as Unix, Linux, Mac OS X, etc. Its function is to create a thread (in fact, to determine the entry point for calling the thread function), and after the thread is created, it starts to run the relevant thread function.
The return value of pthread_create indicates success, returns 0; indicates error, and returns-1.
How the pthread_create function creates threads
Function prototype declaration:
# include
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 from the address of the start_rtn function
Void * restrict arg / / defaults to NULL. If the above function requires parameters, put the parameters in the structure and pass in the address as arg.
);
1. Problems needing attention in passing parameters
Question:
Avoid passing the changed amount directly in the passed parameters, otherwise the result will be unpredictable.
Even if only a single thread is created, it is possible that the value of the variable obtained by the thread has been modified by the main thread when the thread does not get the passing parameters.
Common solution:
Reapply for a piece of memory, store the parameters that need to be passed, and pass in the address as arg.
two。 Pay attention to prevent memory leaks when in use
By default, the thread created by the pthread_create function is non-detached, which is determined by the second parameter of the pthread_create function. In the case of non-separation, when a thread ends, the system resources it occupies are not completely truly released or terminated.
The thread releases its own resources only when the pthread_join function returns.
Or in the case of detaching properties, the end of a thread immediately releases the resources it occupies.
If you want to ensure that there are no memory leaks after creating a thread, you must regulate the use of pthread_create in the following ways:
1. Sets the thread to be detached (detached property)
Void run () {
Return
}
Int main () {
Pthread_t thread
Pthread_attr_t attr
Pthread_attr_init & attr)
Pthread_attr_setdetachstate & attr,1)
Pthread_create (& thread, & attr, run, 0); / / the second parameter determines the separation attribute
/ /.
Return 0
}
However, in the comments on the blog: https://blog.csdn.net/qiurisuixiang/article/details/6648213, someone mentioned:
two。 Use pthread_join function together with it
The pthread_join () function blocks the calling thread until the specified thread terminates. When pthread_join () returns, the application can reclaim any data storage space associated with the terminated thread.
However, at the same time, it should be noted that it must be used with one of the threads created above, so that it can also play the role of mutual exclusion. Otherwise, multithreading may preempt CPU resources, resulting in uncertain running results.
Niu Ke a question: what is the output of the following program? (not sure)
# include
# include
# include
Void* print1 (void* data) {
Printf ("1")
}
Void* print2 (void* data) {
Printf ("2")
}
Void* print3 (void* data) {
Printf ("3")
}
Int main (void) {
Pthread_t t,t1,t2
Pthread_create (& tmeme0print1penol null)
Pthread_create (& t1dir 0dir print2jol null)
Pthread_create (& t2recover0reprint3recovernull)
Pthread_join (tforce null)
Pthread_join (T1 _ null)
Pthread_join (T2 _ null)
Printf ("\ n")
}
Analysis:
Before pthread_join (), all three threads have committed, and they may or may not have been executed sequentially and randomly, so the results are unpredictable. But this can also play the role of recycling memory, right?
/ / this is in order.
Pthread_create (& t, 0, print1, NULL)
Pthread_join (t, NULL)
Pthread_create (& T1, 0, print2, NULL)
Pthread_join (T1, NULL)
Pthread_create (& T2, 0, print3, NULL)
Pthread_join (T2, NULL)
Supplement: pthread_join () function
Function prototype:
Int pthread_join (
Pthread_t tid, / / the thread that needs to wait, the specified thread must be in the current process and must not be a detached thread
Void * * status / / the function executed by thread tid returns a value (the address of the return value needs to be valid), where status can be NULL
);
Default library for pthread non-linux systems, you need to manually link-thread library-lpthread
Return value:
A successful call returns 0.
ESRCH
Description: no thread corresponding to the given thread ID was found. (if multiple threads wait for the same thread to terminate, all waiting threads will wait until the target thread terminates. Then a waiting thread returns successfully. The rest of the waiting threads will fail and return an ESRCH error)
EDEADLK
Description: a deadlock will occur, such as a thread waiting for itself, or thread An and thread B waiting for each other.
EINVAL
Description: the thread corresponding to a given thread ID is a detached thread.
Thank you for your reading, the above is the content of "the usage of multithreaded pthread_create () function". After the study of this article, I believe you have a deeper understanding of the usage of multithreaded pthread_create () function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.