//================================================================================================================================= [1] >> what is thread ? -->><1>TID = Thread identify -->><2>int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg); [ discription : The pthread_create() function starts a new thread in the calling process. 1:The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time to determine(决定) attributes(属性) for the new thread; 2:The new thread starts execution(执行) by invoking(唤醒) start_routine(); 3:arg is passed as the sole(装有的) argument of start_routine(). return:On success, pthread_create() returns 0; on error, it returns an error number ] -->><3>void pthread_exit(void *retval); #The pthread_exit() function terminates the calling thread and returns a value via(通过) retval -->><4> int pthread_join(pthread_t thread, void **retval); [ #discription : The pthread_join() function waits for the thread specified by thread to terminate. If that thread has already terminated, then pthread_join() returns immediately. #node: the value that the target thread supplied to pthread_exit(3)) into the location pointed to by *retval. ] -->><5>synchronization(同步) mass thread [ 1>>多线程采用共享资源采用全局变量(对共享数据进行同步机制,可以保证进程有序使用共享资源) 2>>synchronization:多个任务按照先后次序操作线程间的公共资源,信号量就是一种同步机制,决定线程是运行还是等待 -->>信号量的使用步骤 [ 1>>int sem_init(sem_t *sem, int pshared, unsigned int value); #同步信号量在进程创建之前初始化 [ discription: sem_init() initializes the unnamed semaphore(信号标) at the address pointed to by sem. The value argument specifies the initial value for the semaphore. pshared: If pshared has the value 0, then the semaphore is shared between the threads of a process If pshared is nonzero, then the semaphore is shared between processes } 2>>int sem_wait(sem_t *sem); [ discirption: sem_wait() decrements(减) the semaphore pointed to by sem. If the semaphore's value is greater than zero, then the decrement proceeds, and the function returns, immediately. If the semaphore currently has the value zero, then the call blocks until either it becomes possible to perform(执行) the decrement ] 3>> int sem_post(sem_t *sem); [ discirption: sem_post() increments (加) the semaphore pointed to by sem. If the semaphore's value consequently becomes greater than zero, then another process or thread blocked in a sem_wait(3) call will be woken up and proceed to lock the semaphore. ] ] ] -->><6>mutex(互斥) in mass thread [ 1>>互斥机制使用在共享资源只能同时一个进程使用,防止其他进程修改,但是不能达到信号量同步机制,保证有序使用的效果 2>>互斥锁在进程创建之前初始化 -->>互斥锁的使用步骤 [ 1>>int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); [ discirption: The pthread_mutex_init() function initializes the specified mutex. If attr is non-NULL, the attributes specified are used to initialize the mutex. If attr is NULL, the mutex is initialized with default attributes, ] 2>>int pthread_mutex_lock(pthread_mutex_t *mutex); [ discirption: The mutex object referenced by mutex shall(将会) be locked by calling pthread_mutex_lock(). If the mutex is already locked, the calling thread shall block until the mutex becomes available. ] 3>>int pthread_mutex_unlock(pthread_mutex_t *mutex); #The pthread_mutex_unlock() function shall release the mutex object referenced by mutex. ] ] //=================================================================================================================================