A question that has always been asked: what is the difference between a process and a thread? Perhaps I will answer before:
- Process: Resource allocation minimum unit
- Threads: Lightweight processes are the smallest unit of system scheduling resources that are created by processes to share processes with multiple threads
But now I think a metaphor is better answered: The program is like a stationary train, the process is the running train, and the thread is every carriage that runs the train.
Personal sense of understanding is far better than carrying some conceptual stuff.
One, thread
You can usually include several threads in a process, but there is at least one thread in a process, otherwise there is no meaning. Threads can take advantage of the resources owned by the process, in the introduction of the thread of the operating system, usually the process as the basic unit of allocation of resources, and the thread as a separate operation and independent dispatch of the basic unit, because the thread is smaller than the process, and basically do not have system resources, the scheduling of its costs will be much smaller, Can improve the degree of concurrent execution among multiple programs of the system more efficiently.
1. Thread-related functions
#include <pthread.h>
int Pthread_create (pthread_t *thread, const pthread_attr_t *attr,
void * (*start_routine) (void *), void *arg);
Note: Compile and link with-pthread.//compile with-lpthread
Features: Creating Threads
Parameter 1: ID of the thread
Parameter 2:null
Parameter 3: Thread handler function
Parameter 4: Arguments passed to the threading function
Successfully put back 0 fail back-1
#include <pthread.h>
int Pthread_join (pthread_t thread, void **retval);
Function: Waits for the specified thread in a blocked manner if execution to this function will block waiting for the child thread to end
Program 1-1 demonstrates two function usages:
#include"My.h"void*func (void*p) {*(int*) P =Ten; printf ("%d\n",*(int*) (P);}intMain () {intx= -; pthread_t ID; intret = Pthread_create (&id,NULL,func,&x); if(ret<0) {perror ("pthread_create"); Exit (-1); } pthread_join (Id,null); return 0;}
Note: the header file "My.h" is a custom file, please refer to this blog for details: http://www.cnblogs.com/liudw-0215/p/8946879.html
Run a demo such as:
Second, the signal volume
The semaphore (Semaphore), sometimes called a semaphore, is a facility used in a multithreaded environment that coordinates individual threads to ensure that they are able to use public resources correctly and rationally.
1, the signal volume correlation function
sem_t s;
#include <semaphore.h>
int Sem_init (sem_t *sem, int pshared, unsigned int value);
Format: Sem_init (&s,0,n)
Function: Initializes a semaphore with a value of
Parameter 2: whether to use between processes generally always 0 means not used between processes
Parameter 3: Initial value of the counter
int sem_wait (sem_t *sem);
Reduce the counter by one it will wait until the semaphore has a non-0 value to perform the subtraction operation
If the semaphore with a value of 0 is sem_wait, this function waits until the other thread increments the semaphore so that it is no longer 0
If the sem_wait thread calls on a semaphore with a value of 2, it will continue execution but the semaphore value will be reduced by one.
If two threads are simultaneously waiting on the sem_wait call for the same signal to be a non-0 value then when the semaphore is a third thread +1 there is only one waiting thread to start the semaphore-1
Then continue executing another thread and continue to wait.
#include <semaphore.h>
int Sem_post (sem_t *sem);
Function: Put counter +1
Program 2-1 describes the use of semaphores
#include"My.h"sem_t S;void*fun (void*p) { inti; int*pa = (int*) p; for(i=0;i<4; i++) {sem_wait (&s);//The value of the counter is -1Pa[i] + =i; } for(i=0;i<4; i++) {printf ("%-02d", Pa[i]); } printf ("\ n"); returnNULL;}void*FUN1 (void*2) {sem_wait (&s);//The value of the counter is -1Puts"fun1 run!"); returnNULL;}intMain () {intI=0, ret=0; inta[5]={0}; Sem_init (&s,0,0);//sets the value of the semaphore to 0pthread_t tid[2]; RET= Pthread_create (&tid[0],null,fun,a); if(ret<0) {perror ("pthread_create"); Exit (-1); } ret= Pthread_create (&tid[1],null,fun1,a); if(ret<0) {perror ("pthread_create"); Exit (-1); } for(i=0;i<5; i++) {Sem_post (&s);//The value of the counter is +1} pthread_join (tid[0],null); Pthread_join (tid[1],null); return 0;}
Run a demo such as:
Third, mutual exclusion lock
Mutex: No other thread can access the protected resource as long as it is locked
1. Mutual exclusion lock correlation function
pthread_mutex_t m;
Pthread_mutex_init (&m,null)//Initialize Mutex
Pthread_mutex_lock (&m);//A mutex is locked if the mutex has been locked, the function waits until the thread has unlocked the mutex and then locks it.
Pthread_mutex_unlock (&m);//unlock which line Cheng can only be unlocked by this thread threads unlocked another thread cannot unlock
Program 3-1 demonstrates the mutex application:
#include"My.h"pthread_mutex_t m;void* Thread_fun (void*p) { inti; Pthread_mutex_lock (&M);//Locking for(i=0;i<3; i++) {printf ("pid:%d tid:%lu\n", Getpid (), pthread_self ()); Sleep (1); } pthread_mutex_unlock (&M);//UnlockPthread_exit (NULL);}intMain () {Pthread_mutex_init (&m,null);//Initialize lock intI,ret; pthread_t tid[3]; for(i=0;i<3; i++) {ret= Pthread_create (&tid[i],null,thread_fun,null); if(ret<0) {printf ("pthread_create%d error\n", i); Exit (-1); } } for(i=0;i<3; i++) {pthread_join (tid[i],null); } return 0;}
Run a demo such as:
Summary: The main introduction of thread synchronization of the semaphore and mutual exclusion lock, later time will be more detailed introduction of the implementation of the principle.
Detailed thread semaphore and mutex