Multithreading is a special form of multitasking, and multitasking allows the computer to run two or more programs at the same time (two).
Two types of multitasking: process-based and thread- based ;
Process-based multitasking is the concurrent execution of programs ;
Thread-based multitasking is the concurrent execution of fragments of the same program ;
A multithreaded program contains two or more parts that can run at the same time, each of which is called a thread, and each thread defines a separate execution path;
Create thread : ( function returns 0 when creation is successful)
#include <pthread.h>
Pthread_create (thread, attr, Start_routine, arg)//Pthread_creadte Create a new thread and let it execute
Parameters: Thread: Pointer to the threading identifier;
attr: An opaque Property object that can be used to set thread properties;
Start_routine: The thread runs the start address of the function and executes as soon as the thread is created;
ARG: The argument that runs the function, which must be passed by casting the reference as a pointer to the void type;
To terminate a thread :
#include <pthread.h>
Pthread_exit (status)//Pthread_exit () for the display to exit a thread;
to pass parameters to a thread :
To Connect and detach a thread :
Pthread_join (thread_id, status)//blocks the calling program until the specified thread_id thread terminates;
Pthread_detach (thread_id)
When a thread is created, one of its properties defines whether it is connected (joinable) or separable (detached).
Only threads that are defined as being connected can be connected when they are created;
If a thread is created and is defined as separable, it can never be connected;
C + + Multithreading