POSIX thread学习
POSIX thread学习
1. 问题1, restrict这个关键词干嘛用的?
2. pthread_create()相当于fork().用于产生新线程.一般而言,attr会被设置为NULL.
原型int pthread_create(pthread_t * restrict tidp,
const pthread_attr_t * attr,
void * (*start_rtn)(void *),
void * restrict arg
);
3. pthread_cancel() 相当于kill(),(APUE 2ed.中说相当于abort())用于取消另外一个线程。
原型 int pthread_cancel(pthread_t pid);
3. pthread_exit() 相当于exit(),用于本线程退出.
原型void * pthread_exit(void *rval_ptr);
4. pthread_join()相当于waitpid(),用于取得另外一个线程的退出状态,亦即exit state. pthread_detach()过的线程则不可以再被pthread_join()了.
原型 int pthread_join(pthread_t tid, void **rval_ptr);
Int pthread_detach(pthread_t tid);
5. pthread_cleanup_push()则对应于atexit(),用于注册线程退出时应该调用的cleanup函数.与之对应的是pthread_cleanup_pop().atexit()则无此对应物.
原型 void pthread_cleanup_push(void (cleanup_fcn)(void *), void * arg);
Void pthread_cleanup_pop(int execute);
6. pthread_self()相当于getpid(),它返回调用线程的pthread identifier.
下面是线程同步的笔记.
7. Pthread mutex 名字源于mutual exclusive.
pthread_mutex_t可以用pthread_mutex_init()初始化,也可以用PTHREAD_MUTEX_INITIALIZER初始化.
pthread_mutes_t使用后须用pthread_mutex_destroy()清除.但是注意,pthread_destroy()并不释放内存.比如
pthread_mutex_t *lockp=malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(lockp, NULL);
//此处使用…
pthread_mutex_destroy(lockp);
free(lockp);
最后一行的free是不能被倒数第二行的destroy替代的.
问题:destroy究竟释放些什么资源?有时间研究下其实现.
8. pthread_mutex_lock()与pthread_mutex_trylock()的区别在,后者不会block,假如锁已经被其他线程取得,则后者会返回EBUSY. pthread_mutex_unlock()用于释放锁,
9. 还有Reader_Writer Lock,比较烦琐,详见APUE 2ed P379.
10. 关于condition variable,参见下一篇帖子
1. 问题1, restrict这个关键词干嘛用的?
2. pthread_create()相当于fork().用于产生新线程.一般而言,attr会被设置为NULL.
原型int pthread_create(pthread_t * restrict tidp,
const pthread_attr_t * attr,
void * (*start_rtn)(void *),
void * restrict arg
);
3. pthread_cancel() 相当于kill(),(APUE 2ed.中说相当于abort())用于取消另外一个线程。
原型 int pthread_cancel(pthread_t pid);
3. pthread_exit() 相当于exit(),用于本线程退出.
原型void * pthread_exit(void *rval_ptr);
4. pthread_join()相当于waitpid(),用于取得另外一个线程的退出状态,亦即exit state. pthread_detach()过的线程则不可以再被pthread_join()了.
原型 int pthread_join(pthread_t tid, void **rval_ptr);
Int pthread_detach(pthread_t tid);
5. pthread_cleanup_push()则对应于atexit(),用于注册线程退出时应该调用的cleanup函数.与之对应的是pthread_cleanup_pop().atexit()则无此对应物.
原型 void pthread_cleanup_push(void (cleanup_fcn)(void *), void * arg);
Void pthread_cleanup_pop(int execute);
6. pthread_self()相当于getpid(),它返回调用线程的pthread identifier.
下面是线程同步的笔记.
7. Pthread mutex 名字源于mutual exclusive.
pthread_mutex_t可以用pthread_mutex_init()初始化,也可以用PTHREAD_MUTEX_INITIALIZER初始化.
pthread_mutes_t使用后须用pthread_mutex_destroy()清除.但是注意,pthread_destroy()并不释放内存.比如
pthread_mutex_t *lockp=malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(lockp, NULL);
//此处使用…
pthread_mutex_destroy(lockp);
free(lockp);
最后一行的free是不能被倒数第二行的destroy替代的.
问题:destroy究竟释放些什么资源?有时间研究下其实现.
8. pthread_mutex_lock()与pthread_mutex_trylock()的区别在,后者不会block,假如锁已经被其他线程取得,则后者会返回EBUSY. pthread_mutex_unlock()用于释放锁,
9. 还有Reader_Writer Lock,比较烦琐,详见APUE 2ed P379.
10. 关于condition variable,参见下一篇帖子
Labels: linux
0 Comments:
Post a Comment
<< Home