Linux pthread线程同步相关的API学习
原因
最近在深入理解Dalvik虚拟机的内部线程控制体系,其中涉及大量pthread的相关操作,现在统一浏览一下。
?
#include
gcc -lpthread xxx.c -o xxx.out
主要API:
- 标识
- int pthread_equal(pthread_t tid1, pthread_t tid2);//若相等则返回非0值,否则返回0
- pthread_t pthread_self(void);
-
创建
- int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void*(*start_rtn)(void*), void* restrict arg);//success return 0, else return errno
-
终止
- void pthread_exit(void *return_value_ptr);
- int pthread_join(pthread_t the_exit_pthread_, void ** return_value_ptr_ptr);
- int pthread_cancel(pthread_t tid);
- void pthread_cleanup_push(void (*rtn)(void*), void* arg);
- void pthread_cleanup_pop(int execute);
- int pthread_detach(pthread_t tid);
- 同步
- 互斥量
- int pthread_mutex_init(pthread_mutex_t*restrict mutex, const pthread_mutexattr_t * restrict attr);
- int pthread_mutex_destroy(phtread_mutex_t *mutex);//success if return 0, else return errno
- int pthread_mutex_lock(pthread_mutex_t * mutex);
- int pthread_mutex_trylock(pthread_mutex_t * mutex);//异步
- int pthread_mutex_unlock(pthread_mutex_t * mutex);
- 同步信号量