Linux thread Summary - thread creation, exit, cancellation, recycling and separation properties

1, Basic concepts of threads

	1,Basic concepts
	Threads are special processes. In the operating system, threads cannot exist independently. Threads are created by processes. A process can have multiple threads. If the process exits, the thread will also exit.
	2,resources
	Each process has its own independent heap, stack, data segment, code segment and other space. Threads basically have no independent resources, only essential resources (stack). Threads between the same process share all resources in the process.

2, Thread related API s

1. Thread creation function

Thread: the id of the stored thread
attr: thread separation attribute - > null
start_routine: the parameter is void *, and the return value is also a function pointer of void * type( Thread execution function)
arg: parameter of function
Return value: 0 for success and non-0 for failure
Code snippet:

void * Pthread_Task(void *arg)// arg = data;
{
	int a = (long)arg;
	printf("-----%d\n", a);
}
int main()
{
    long data = 110;
    //Create thread
	pthread_t pid;
	int ret = pthread_create(&pid, NULL, Pthread_Task, (void *)data);
	if(ret != 0)
	{
		perror("ptread_create");
		return -1;
	}
	else
	{
		printf("Thread creation succeeded!\n");
	}
}

2. Thread exit

Return: returns the end state of the thread (void * variable)
There are three ways to exit a thread:

  • After calling the task function of the thread, return to exit (directly die), and let others recycle the thread resources for the dead thread. The thread calls the pthread_exit();
    Immediately die, let others recycle thread resources for this dead thread
  • Cancel thread pthreada_cancel: just a request (there is no guarantee that the thread will exit)
  • Set the thread bit separation property. He doesn't need someone to collect his body when he dies.
    3. Thread resource recovery function

    Thread: id of the child thread
    retval: child thread end status
    Wait to reclaim the resources (stack space) of the child thread, which is equivalent to the waitpid in the child thread
    pthread_join: it is blocked by default. You cannot call pthread yourself_ Join to recycle yourself.
    4. Set thread separation properties

    1) Separation attribute: the resources of the sub thread are recycled by the system. Pthread is required for resource recycling of the thread_ Join. If the thread does not finish running, it will block the main thread. When the main thread needs to create a new thread to do something, the main thread will call pthread_ If the join is blocked, there is no way to handle other transactions. Therefore, the separation property of the thread is introduced. It does not need to be recycled by the main thread, and it will be recycled automatically after exiting the system.
    Thread separation function:
    int pthread_detach(pthread_t thread);
    Thread: thread id
    2) Select the separation property when creating a thread ----- for pthread_ Set the second parameter of create
    pthread_attr_t *attr
    Define a pthread_attr_t type variable attr, and then initialize the variable attr pthread_attr_init() finally sets the detach attribute.
    ① Initialize thread properties
    int pthread_attr_init(pthread_attr_t *attr);
    ② Set separation properties
    int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
    5. Thread cancellation



    1) Thread cancellation function: pthread_cancel()
    2) Set thread cancel response - > whether to respond to cancel signal
    3) Set the type of response cancellation signal ----- > immediate response and delayed response
    Code snippet:
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
struct Data
{
    char pthread_name[10];
};

void *Pthread_Task(void *arg)
{
    //Set thread cancellation status --- accept cancellation request
    int pthread_setcancelstate_ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    if (pthread_setcancelstate_ret != 0)
    {
        perror("pthread_setcancelstate");
        exit(-1);
    }
    while (1)
    {
        struct Data *p = (struct Data *)arg;
        printf("%s\n", p->pthread_name);
        sleep(1);
    }
    pthread_exit(NULL);
}

int main()
{
    pthread_t pid;
    struct Data d1;
    memset(&d1, 0, sizeof(d1));
    strcpy(d1.pthread_name, "hello");
    //Create thread
    int ret = pthread_create(&pid, NULL, Pthread_Task, (void *)&d1);
    if (ret != 0)
    {
        perror("pthread_create");
        exit(-1);
    }
    printf("5s Then send a cancellation request\n");
    sleep(5);
    //Cancel thread
    pthread_cancel(pid);
    pause();
    return 0;
}

Pressing stack and elastic stack should be used together. Whoever wants to respond to the registration function is the one who writes pressing stack and elastic stack.
Code snippet:

#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

//Thread registration function
void Pthread_At_Fun(void *arg)
{
    printf("%s\n", (char *)arg);
}
//Thread task function
void *Pthread_Task(void *arg)
{   
    //Stack pressing
    pthread_cleanup_push(Pthread_At_Fun, (void *)"Death flash");
    printf("I will die in three seconds\n");
    sleep(3);
    pthread_exit(NULL);
    //Bomb stack
    pthread_cleanup_pop(0);  
}
int main()
{
    //Create thread
    pthread_t pid;
    int pthread_create_ret = pthread_create(&pid, NULL, Pthread_Task, NULL);
    if(pthread_create_ret != 0)
    {
        perror("pthread_create");
        exit(-1);
    }
    while(1)
    {
        printf("I saw the death flash of the small thread!\n");
        sleep(1);
    }
    return 0;
}

Keywords: Linux

Added by iceangel89 on Tue, 07 Sep 2021 05:46:42 +0300