LINUX multi-thread function analysis
LINUX multi-threaded functions use the following three
Pthread_create (), pthread_exit (), pthread_join (); they are all in the header file. Static library-lpthread must be added during compilation
The following describes the functions:
Pthread_create is a thread function created in a Unix environment.
Int pthread_create (
Pthread_t * restrict TIDP,
Const pthread_attr_t * restrict_attr,
Void * (* start_rtn) (void *),
Void * restrict Arg );
Return Value
If the call succeeds, 0 is returned; otherwise, an error number is returned.
When a success is returned, the memory unit pointed to by TIDP is set as the thread ID of the newly created thread. The ATTR parameter is used to define various thread attributes. The newly created thread starts running from the address of the start_rtn function. This function only has the 10 thousand pointer parameter Arg. If more than one parameter needs to be passed to the start_rtn function, you need to put these parameters in a structure, and then pass the address of this structure as the ARG parameter.
Multi-threaded programs are developed in C in Linux. multithreading in Linux follows the POSIX thread interface, which is called pthread.
The Pointer Modified by restrict is the first and only method to access the object pointed to by the pointer. Only when the second pointer is based on the first one can the object be accessed. Access to objects is limited to pointer expressions modified by restrict. Pointers modified by restrict are mainly used for function parameters or memory space allocated by malloc. The restrict data type does not change the semantics of the program. The compiler can better optimize some types of routines by making the restrict modifier pointer as the assumption that the only method to access the object is used.
Parameters
The first parameter is the pointer to the thread identifier.
The second parameter is used to set the thread attributes.
The third parameter is the starting address of the thread-running function.
The last parameter is the parameter used to run the function.
In addition, add the-lpthread parameter during compilation to call the static Link Library. Because pthread is not the default library in Linux
Pthread_exit (void * retval );
The thread terminates its own execution by calling the pthread_exit function, just as the process calls the exit function at the end. This function terminates the thread that calls it and returns a pointer to an object. The pointer can be obtained through the second value_ptr parameter in pthread_join (pthread_t tpid, void ** value_ptr.
The pthread_join function is used to wait for the end of a thread. Function prototype:
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, which can be used to store the return value when the waiting thread exits. This function is a thread-blocking function. The function called will wait until the end of the waiting thread. When the function returns, the resources of the waiting thread will be reclaimed. If the execution is successful, 0 is returned. If the execution fails, an error number is returned.
All threads have a thread ID. Its type is pthread_t. Call the pthread_self () function to obtain its own thread number.
The following is a simple example. The sub-thread thread_fun will play "This is thread_fun print!" five times !" Then call pthread_exit to exit and return a string pointing to "this is thread Return Value !" Pointer. Call pthread_join in the main function and wait until the thread_fun thread ends. Then, read the return value of the subthread to the value and print it out.
The output result is:
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!
#include#include#include#include#include#include//////////////////////////////////////////////////////void *thread_fun(void *arg) { int i=0; char *value_ptr = "this is thread return value!\n"; for (i=0; i<5; i++) { printf("this is thread_fun print!\n"); sleep(1); } pthread_exit((void*)value_ptr);}//////////////////////////////////////////////////////int main(int argc, char **argv) { pthread_t pid; int ret; void* value; ret = pthread_create(&pid, NULL, thread_fun, NULL); if (ret) { printf("pthread_create failed!\nerrno:%d\n", errno); return -1; } printf("pthread_create ok!\n"); pthread_join(pid, &value); printf("pthread exit value: %s\n", value); return 0;}
Reprinted http://software.intel.com/zh-cn/blogs/2012/02/14/linux-4? Cid = Sw: prccsdn2159