Article title: Linux multi-threaded programming basic functions. Linux is a technology channel of the IT lab in China. Including desktop applications, Linux system management, kernel research, embedded systems, open source, and other basic categories 1 thread creation
Function prototype:
# Include
Int pthread_create (pthread_t * restrict tidp, const pthread_attr_t * restrict attr, void * (* start_rtn) (void), void * restrict arg );
Returned value: if a thread is successfully created, 0 is returned. Otherwise, the error number is returned.
Format parameters:
Pthread_t * restrict tidp indicates the thread id pointer of the thread to be created
Const pthread_attr_t * restrict attr specifies the thread attributes when a thread is created.
Void * (start_rtn) (void) returns a void pointer function.
Vodi * restrict arg start_rtn row parameter
Example 1:
Function: Test the creation of a new thread.
Program Name: pthread_test.c
# Include
# Include
Void * create (void * arg)
...{
Printf ("new thread created .....");
}
Int main (int argc, char * argv [])
...{
Pthread_t tidp;
Int error;
Error = pthread_create (& tidp, NULL, create, NULL );
If (error! = 0)
......{
Printf ("pthread_create is not created ...");
Return-1;
}
Printf ("prthread_create is created ...");
Return 0;
}
Compilation method:
# Gcc-Wall-lpthread pthread_test.c
Because the pthread library is not a linux system library, you must add-lpthread during compilation. Otherwise, the compilation will fail and the following error will occur.
Thread_test.c:In the 'create' function'Medium:
Thread_test.c: 7:Warning in a function with returned values, the program flow reaches the end of the function.
/Tmp/ccOBJmuD. o: In function 'main': thread_test.c :(. text + 0x4f): For 'pthread _ create'Undefined reference
Collect2: ldReturn 1
[1] [2] [3] [4] [5] [6] Next page