Linux system programming: thread primitives, linux programming thread primitives
Thread primitive
Thread Concept
A thread, sometimes called a Lightweight Process (LWP), is the smallest unit of a program execution flow. A standard thread consists of the thread ID, current Instruction Pointer (PC), register set, and stack. For more details, see Baidu Encyclopedia: thread.
In Linux shell, run the $ ps-Lf pid command to view all threads under the specified pid.
Sharing and non-sharing between threads
The thread here refers to the thread under the same process.
Share:
1. file descriptor table
2. Processing Methods of each signal
3. current working directory
4. User ID and group ID
5. Memory Address Space
Non-shared:
1. Thread id
2. Processor field and stack pointer (kernel stack)
3. Independent stack space (user space stack)
4. errno variable
5. signal shielding characters
6. Scheduling Priority
Thread primitive
Run $ man-k pthread to view all functions related to threads in the system.
Generally, the main function is called the main thread or the main control thread, and other threads are called subthreads.
Create thread
#include <pthread.h>int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
Pthread_t * thread: pass in a pthread_t variable address to save the tid (thread ID) of the new thread)
Const pthread_attr_t * attr: Specifies the thread attribute. If the default attribute is used, NULL is passed.
Void * (* start_routine) (void *): function pointer, pointing to the function module that should be loaded and executed by the new thread.
Void * arg: Specifies the parameter of the function to be loaded by the thread.
Return Value: 0 is returned for success, and an error number is returned for failure. All system functions that have been learned previously return 0 for success,-1 for failure, and the error number is saved in the global variable errno. All the functions in the pthread library return error numbers through the return value, although each thread also has an errno, it is provided to be compatible with other function interfaces. The pthread library itself does not use it, and the error code returned through the return value is clearer.
Compile and link with-lpthread.
Typedef unsigned long int pthread_t; // pthread_t may be implemented differently in different systems
End thread
If you only need to terminate a thread without terminating the entire process, there are three methods:
1. return. This method is not applicable to the main control thread. return from the main function is equivalent to calling exit.
2. One thread can call pthread_cancel to terminate another thread in the same process.
#include <pthread.h>int pthread_cancel(pthread_t thread);
The exit value of the canceled thread. The value of the constant PTHREAD_CANCELED in the Linux pthread library is-1. You can find its definition in the header file pthread. h: # define PTHREAD_CANCELED (void *)-1 ). That is to say, the exit value of the thread terminated by pthread_cancel is-1.
Between threads of the same process, pthread_cancel sends a termination signal to the other line. The system will not immediately shut down the canceled thread. It will actually end the thread only when the canceled thread is called by the system next time. Or call pthread_testcancel to let the kernel check whether the current thread needs to be canceled.
3. The thread can call pthread_exit to terminate itself.
# Include <pthread. h> void pthread_exit (void * retval); void * retval: The parameter passed when the thread exits. It can be an exit value or address. If it is an address, it cannot be a local address applied by the thread.
Calling exit anywhere will cause the entire process to exit.
Thread id
#include <pthread.h>pthread_t pthread_self(void);RETURN VALUEThis function always succeeds, returning the calling thread’s ID.
Use pthread_self in the thread to obtain the thread id.
Recycle thread
# Include <pthread. h> int pthread_join (pthread_t thread, void ** retval); pthread_t thread: reclaim the thread's tidvoid ** retval: receive the returned value from the exit thread: 0 is returned for success, error Code returned for failure
Similar to the process, after the thread exits, it also needs to be recycled, so pthread_join is similar to wait. The thread that calls this function will wait until the thread whose id is thread terminates. The thread terminates in different ways. The termination status obtained through pthread_join is different. The following is a summary:
If you are not interested in the termination status of the thread, you can pass null to the retval parameter.
Code example: Use return and pthread_exit to end the process
//thread_exit.c#include <stdio.h>#include <unistd.h>#include <pthread.h>void *fun_1(void *argv){printf("Hello %s,my thread id is %x\n", (char*)argv, pthread_self());sleep(1);//pthread_exit((void*)1);return (void*)1;}void *fun_2(void *argv){printf("Hello %c, my thread id is %x\n", (char)argv, pthread_self());sleep(2);pthread_exit((void*)"David");}int main(void){int *ret1;char *ret2;pthread_t tid1, tid2;pthread_create(&tid1, NULL, fun_1, (void*)"zhangxiang");pthread_create(&tid2, NULL, fun_2, (void*)'D');pthread_join(tid1, (void*)&ret1);pthread_join(tid2, (void*)&ret2);printf("thread %x exit with %x\n", tid1, ret1);printf("thread %x exit with %s\n", tid2, ret2);return 0;}$ gcc thread_exit.c -lpthread$ ./a.outHello D, my thread id is 745e9700Hello zhangxiang, my thread id is 74fea700thread 74fea700 exit with 1thread 745e9700 exit with David
Use pthread_cancel to terminate another thread
//thread_cancel.c#include <stdio.h>#include <pthread.h>void *fun(void *argv){printf("thread %x running\n", pthread_self());pthread_exit((void*)0);}int main(void){pthread_t tid;void *ret;pthread_create(&tid, NULL, fun, NULL);pthread_join(tid, &ret);printf("thread %x exit with %d\n", tid, (int)ret);pthread_create(&tid, NULL, fun, NULL);pthread_cancel(tid);pthread_join(tid, &ret);printf("thread %x exit with %d\n", tid, (int)ret);return 0;}$ gcc thread_cancel.c -lpthread$ a.outthread 96da9700 runningthread 96da9700 exit with 0thread 96da9700 runningthread 96da9700 exit with -1
You can use pthread_cancel to end a subthread in the main thread, or terminate another subthread in the subthread.
CCPP Blog directory
Copyright Disclaimer: This article is an original article by the blogger. For more information, see the source.