一.头文件
#include <pthread.h>
二.编译选项
-lpthread
三.结构体
pthread_t pthread_attr_t pthread_barrier_t pthread_barrierattr_t pthread_cond_t pthread_condattr_t pthread_key_t pthread_mutex_t pthread_mutexattr_t pthread_once_t pthread_rwlock_t pthread_rwlockattr_t pthread_spinlock_t
四.API函数
pthread_create() - thread creation //创建线程 int pthread_create(pthread_t *restrict thread,const pthread_attr_t *restrict attr,void *(*start_routine)(void*), void *restrict arg); pthread_detach() - detach a thread //解绑线程(子线程退出可以回收资源) int pthread_detach(pthread_t thread); pthread_join() - wait for thread termination //等待子进程结束 int pthread_join(pthread_t thread, void **value_ptr); pthread_exit() - thread termination //线程退出 void pthread_exit(void *value_ptr); pthread_self() - get calling thread's ID //获取线程自身id pthread_t pthread_self(void); pthread_kill() - send a signal to a thread //发送结束信号给线程 int pthread_kill(pthread_t thread, int sig); pthread_equal() - compare thread IDs //比较线程id int pthread_equal(pthread_t t1, pthread_t t2); pthread_cancel() - cancel execution of a thread //取消线程 int pthread_cancel(pthread_t thread); pthread_cleanup_pop(), pthread_cleanup_push - establish cancelation handlers void pthread_cleanup_pop(int execute); void pthread_cleanup_push(void (*routine)(void*), void *arg); pthread_getcpuclockid() - access a thread CPU-time clock (ADVANCED REALTIME THREADS) int pthread_getcpuclockid(pthread_t thread_id, clockid_t *clock_id); pthread_once() - dynamic package initialization int pthread_once(pthread_once_t *once_control,void (*init_routine)(void)); pthread_atfork() - register fork handlers int pthread_atfork(void (*prepare)(void), void (*parent)(void),void (*child)(void)); pthread_getconcurrency() - get level of concurrency int pthread_getconcurrency(void); pthread_setconcurrency() - set level of concurrency int pthread_setconcurrency(int new_level); pthread_getschedparam() - dynamic thread scheduling parameters access (REALTIME THREADS) int pthread_getschedparam(pthread_t thread, int *restrict policy,struct sched_param *restrict param); pthread_setschedparam() - dynamic thread scheduling parameters access (REALTIME THREADS) int pthread_setschedparam(pthread_t thread, int policy,const struct sched_param *param); pthread_getspecific() - thread-specific data management void *pthread_getspecific(pthread_key_t key); pthread_setspecific() - thread-specific data management int pthread_setspecific(pthread_key_t key, const void *value); pthread_key_create() - thread-specific data key creation int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)); pthread_key_delete() - thread-specific data key deletion int pthread_key_delete(pthread_key_t key); pthread_setcancelstate() - set cancelability state int pthread_setcancelstate(int state, int *oldstate); pthread_setcanceltype() - set cancelability state int pthread_setcanceltype(int type, int *oldtype); pthread_testcancel() - set cancelability state void pthread_testcancel(void); pthread_setschedprio() - dynamic thread scheduling parameters access (REALTIME THREADS) int pthread_setschedprio(pthread_t thread, int prio); pthread_sigmask() - examine and change blocked signals int pthread_sigmask(int how, const sigset_t *restrict set,sigset_t *restrict oset); sigprocmask() - examine and change blocked signals int sigprocmask(int how, const sigset_t *restrict set,sigset_t *restrict oset); pthread_attr_init() - initialize threads attributes object int pthread_attr_init(pthread_attr_t *attr); pthread_attr_destroy() - destroy threads attributes object int pthread_attr_destroy(pthread_attr_t *attr); pthread_attr_getinheritsched() - get inheritsched attribute (REALTIME THREADS) int pthread_attr_getinheritsched(const pthread_attr_t *restrict attr,int *restrict inheritsched); pthread_attr_setinheritsched() - set inheritsched attribute (REALTIME THREADS) int pthread_attr_setinheritsched(pthread_attr_t *attr,int inheritsched); pthread_attr_getschedparam() - get schedparam attribute int pthread_attr_getschedparam(const pthread_attr_t *restrict attr,struct sched_param *restrict param); pthread_attr_setschedparam() - set schedparam attribute int pthread_attr_setschedparam(pthread_attr_t *restrict attr,const struct sched_param *restrict param); pthread_attr_getschedpolicy() - get schedpolicy attribute (REALTIME THREADS) int pthread_attr_getschedpolicy(const pthread_attr_t *restrict attr,int *restrict policy); pthread_attr_setschedpolicy() - set schedpolicy attribute (REALTIME THREADS) int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); pthread_attr_getscope() - get contentionscope attribute (REALTIME THREADS) int pthread_attr_getscope(const pthread_attr_t *restrict attr,int *restrict contentionscope); pthread_attr_setscope() - set contentionscope attribute (REALTIME THREADS) int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope); pthread_attr_getstackaddr() - get stackaddr attribute int pthread_attr_getstackaddr(const pthread_attr_t *restrict attr,void **restrict stackaddr); pthread_attr_setstackaddr() - set stackaddr attribute int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr); pthread_attr_getstack() - set stack attributes int pthread_attr_getstack(const pthread_attr_t *restrict attr,void **restrict stackaddr, size_t *restrict stacksize); pthread_attr_setstack() - set stack attribute int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,size_t stacksize); pthread_attr_getdetachstate() - get detachstate attribute int pthread_attr_getdetachstate(const pthread_attr_t *attr,int *detachstate); pthread_attr_setdetachstate() - set detachstate attribute int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); pthread_attr_getguardsize() - get thread guardsize attribute int pthread_attr_getguardsize(const pthread_attr_t *restrict attr,size_t *restrict guardsize); pthread_attr_setguardsize() - set thread guardsize attribute int pthread_attr_setguardsize(pthread_attr_t *attr,size_t guardsize); pthread_attr_setstacksize() - set stacksize attribute int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); pthread_attr_getstacksize() - get stacksize attribute int pthread_attr_getstacksize(const pthread_attr_t *restrict attr,size_t *restrict stacksize); pthread_barrier_init() - initialize a barrier object (ADVANCED REALTIME THREADS) int pthread_barrier_init(pthread_barrier_t *restrict barrier,const pthread_barrierattr_t *restrict attr, unsigned count); pthread_barrier_destroy() - destroy a barrier object (ADVANCED REALTIME THREADS) int pthread_barrier_destroy(pthread_barrier_t *barrier); pthread_barrier_wait() - synchronize at a barrier (ADVANCED REALTIME THREADS) int pthread_barrier_wait(pthread_barrier_t *barrier); pthread_barrierattr_init() - initialize barrier attributes object (ADVANCED REALTIME THREADS) int pthread_barrierattr_init(pthread_barrierattr_t *attr); pthread_barrierattr_destroy() - destroy barrier attributes object (ADVANCED REALTIME THREADS) int pthread_barrierattr_destroy(pthread_barrierattr_t *attr) pthread_barrierattr_getpshared(), pthread_barrierattr_setpshared - get and set process-shared attribute of barrier attributes object (ADVANCED REALTIME THREADS) int pthread_barrierattr_getpshared(const pthread_barrierattr_t *restrict attr, int *restrict pshared); pthread_barrierattr_setpshared() - set process-shared attribute of barrier attributes object (ADVANCED REALTIME THREADS) int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr,int pshared); pthread_condattr_init() - initialize condition variable attributes object int pthread_condattr_init(pthread_condattr_t *attr); pthread_condattr_destroy() - destroy condition variable attributes object int pthread_condattr_destroy(pthread_condattr_t *attr); pthread_condattr_getclock() - get the clock selection condition variable attribute (ADVANCED REALTIME) int pthread_condattr_getclock(const pthread_condattr_t *restrict attr,clockid_t *restrict clock_id); pthread_condattr_setclock() - set the clock selection condition variable attribute int pthread_condattr_setclock(pthread_condattr_t *attr,clockid_t clock_id); pthread_condattr_getpshared() - get the process-shared condition variable attributes int pthread_condattr_getpshared(const pthread_condattr_t *restrict attr,int *restrict pshared); pthread_condattr_setpshared() - set the process-shared condition variable attributes int pthread_condattr_setpshared(pthread_condattr_t *attr,int pshared); pthread_cond_init() - initialize condition variables int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr); pthread_cond_destroy() - destroy condition variables int pthread_cond_destroy(pthread_cond_t *cond); pthread_cond_broadcast() - broadcast a condition int pthread_cond_broadcast(pthread_cond_t *cond); pthread_cond_signal() - signal a condition int pthread_cond_signal(pthread_cond_t *cond); pthread_cond_timedwait() - wait on a condition int pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex,const struct timespec *restrict abstime); pthread_cond_wait() - wait on a condition int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex); pthread_mutexattr_init() - initialize mutex attributes object int pthread_mutexattr_init(pthread_mutexattr_t *attr); pthread_mutexattr_destroy() - destroy mutex attributes object int pthread_mutexattr_destroy(pthread_mutexattr_t *attr); pthread_mutexattr_getprioceiling() - get prioceiling attribute of mutex attributes object (REALTIME THREADS) int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *restrict attr, int *restrict prioceiling); pthread_mutexattr_setprioceiling() - set prioceiling attribute of mutex attributes object (REALTIME THREADS) int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr,int prioceiling); pthread_mutexattr_getprotocol() - get and set protocol attribute of mutex attributes object (REALTIME THREADS) int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *restrict attr, int *restrict protocol); pthread_mutexattr_setprotocol() - set protocol attribute of mutex attributes object (REALTIME THREADS) int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr,int protocol); pthread_mutexattr_getpshared() - get process-shared attribute int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict attr, int *restrict pshared); pthread_mutexattr_setpshared() - set process-shared attribute int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr,int pshared); pthread_mutexattr_gettype() - get a mutex type attribute int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr,int *restrict type); pthread_mutexattr_settype() - set a mutex type attribute int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type); pthread_mutex_init() - initialize a mutex int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr); pthread_mutex_destroy() - destroy a mutex int pthread_mutex_destroy(pthread_mutex_t *mutex); pthread_mutex_getprioceiling() - get the priority ceiling of a mutex (REALTIME THREADS) int pthread_mutex_getprioceiling(const pthread_mutex_t *restrict mutex,int *restrict prioceiling); pthread_mutex_setprioceiling() - change the priority ceiling of a mutex (REALTIME THREADS) int pthread_mutex_setprioceiling(pthread_mutex_t *restrict mutex,int prioceiling, int *restrict old_ceiling); pthread_mutex_lock() - lock a mutex int pthread_mutex_lock(pthread_mutex_t *mutex); pthread_mutex_trylock() - lock a mutex int pthread_mutex_trylock(pthread_mutex_t *mutex); pthread_mutex_timedlock() - lock a mutex (ADVANCED REALTIME) int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex,const struct timespec *restrict abs_timeout); pthread_mutex_unlock() - unlock a mutex int pthread_mutex_unlock(pthread_mutex_t *mutex); pthread_rwlockattr_init() - initialize read-write lock attributes object int pthread_rwlockattr_init(pthread_rwlockattr_t *attr); pthread_rwlockattr_destroy() - destroy read-write lock attributes object int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr); pthread_rwlockattr_getpshared() - get process-shared attribute of read-write lock attributes object int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict attr, int *restrict pshared); pthread_rwlockattr_setpshared() - set process-shared attribute of read-write lock attributes object int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr,int pshared); pthread_rwlock_init() - initialize a read-write lock object int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr); pthread_rwlock_destroy() - destroy a read-write lock object int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); pthread_rwlock_rdlock() - lock a read-write lock object for reading int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); pthread_rwlock_wrlock() - lock a read-write lock object for writing int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); pthread_rwlock_tryrdlock() - lock a read-write lock object for reading int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock); pthread_rwlock_trywrlock() - lock a read-write lock object for writing int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock); pthread_rwlock_timedrdlock() - lock a read-write lock for reading int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock,const struct timespec *restrict abs_timeout); pthread_rwlock_timedwrlock() - lock a read-write lock for writing int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock,const struct timespec *restrict abs_timeout); pthread_rwlock_unlock() - unlock a read-write lock object int pthread_rwlock_unlock(pthread_rwlock_t *rwlock); pthread_spin_init() - initialize a spin lock object (ADVANCED REALTIME THREADS) int pthread_spin_init(pthread_spinlock_t *lock, int pshared); pthread_spin_destroy() - destroy a spin lock object (ADVANCED REALTIME THREADS) int pthread_spin_destroy(pthread_spinlock_t *lock); pthread_spin_lock() - lock a spin lock object (ADVANCED REALTIME THREADS) int pthread_spin_lock(pthread_spinlock_t *lock); pthread_spin_trylock() - lock a spin lock object (ADVANCED REALTIME THREADS) int pthread_spin_trylock(pthread_spinlock_t *lock); pthread_spin_unlock() - unlock a spin lock object (ADVANCED REALTIME THREADS) int pthread_spin_unlock(pthread_spinlock_t *lock);
五.例子
一个比较综合的例子
#include <stdio.h> #include <pthread.h> #include <sys/time.h> #include <stdlib.h> pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; static void* thread(void* prm) { struct timeval now; struct timespec timeout; char *arg=(char*)prm; printf("input :%s\n",arg); //打印传递的参数 pthread_mutex_lock(&mut); //加锁线程互斥锁 gettimeofday(&now,NULL); timeout.tv_sec=now.tv_sec + 5; timeout.tv_nsec=now.tv_sec * 1000; pthread_cond_timedwait(&cond, &mut, &timeout); //线程条件等待5s pthread_mutex_unlock(&mut); //解锁线程互斥锁 printf("This is Child pthread\n"); sleep(5); pthread_detach(pthread_self()); //获取线程自身id,接绑线程,结束可释放资源 } int main(int argc,char **argv) { pthread_t id; int ret; char *arg=(char *)malloc(255); sprintf(arg,"no input argv!\n"); if(argc==2){ sprintf(arg,"%s",argv[1]); } ret=pthread_create(&id,NULL,(void *) thread,(void*)arg); //创建线程,传递参数 if(ret!=0){ printf ("Create pthread error!\n"); exit (1); } printf("This is main process\n"); pthread_join(id,NULL); //等待子线程结束 printf("Child thread done!\n"); free(arg); return (0); }