pthread, POSIX线程。
线程间的切换的话,不用像进程间那样需要完整的上下文切换,从而节省开销。
__clone函数调用
__clone的目的是为了更容易实现pthread库。
Linux私有函数__clone是fork的替代函数,她能够更多控制父进程和子进程之间共享那些进程资源。
#include
int __clone(int (*fn)(void *fnarg), void *child_stack, int flags, void *arg);
pthread接口
pthread = c函数调用 数据结构 libpthread.so
pthread_create
#include
int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
pthread_exit
void pthread_exit(void *retval);
pthread_join
挂起当前线程,直到th指定的线程终止运行为止。
int pthread_join(pthread_t th, void **thread_return);
int pthread_detach(pthread_t th);
一个线程所使用的内存资源在对该线程应用pthread_join之前不会被重新分配,所以对于可切入的线程必须调用一次join函数。
pthread_cancel
int pthread_setcancelstate(int state, it *oldstate);
int pthread_cancel(pthread_t thread);
code sample for pthread_create
- /*
- * thrdcreat.c - Illustate creating a thread
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- void err_quit(char *msg);
- void task1(int *counter);
- void task2(int *counter);
- void cleanup(int counter1, int counter2);
- int g1 = 0;
- int g2 = 0;
- int main(int argc, char *argv[]) {
- pthread_t thrd1, thrd2;
- int ret;
- ret = pthread_create(&thrd1, NULL, (void *)task1, (void *)&g1);
- if (ret) err_quit("pthred_create: task1");
- ret = pthread_create(&thrd2, NULL, (void *)task2, (void *)&g2);
- if (ret) err_quit("pthred_create: task2");
- pthread_join(thrd1, NULL);
- pthread_join(thrd2, NULL);
- cleanup(g1, g2);
- exit(EXIT_SUCCESS);
- }
- void task1(int *counter) {
- while(*counter < 5) {
- printf("task1 count: %d\n", *counter);
- (*counter) ;
- sleep(1);
- }
- }
- void task2(int *counter) {
- while(*counter < 5) {
- printf("task2 count: %d\n", *counter);
- (*counter) ;
- }
- }
- void cleanup(int counter1, int counter2) {
- printf("total :%d\n", counter1 counter2);
- }
- void err_quit(char *msg) {
- perror(msg);
- exit(EXIT_FAILURE);
- }
pthread条件
用于挂起当前线程知道满足条件为止。
#include
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_timewait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);
int pthread_cond_destroy(pthread_cond_t *cond);
singal & broadcast用于重启等待条件的线程。
wait & timewait 解开mutex指出的一个mutex, 然后等待变量cond上的信号。
互斥
mutex = mutual exclusion, 是一种锁或者信号灯。
一个互斥锁只有两种状态: locked , unlocked.
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t * mutexatrr);
int pthread_mutex_lock(pthread_mutex_t * mutex);
int pthread_mutex_trylock(pthrad_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
code sample for mutex:
- /*
- * mutex.c - using mutex
- */
- #include <pthread.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #define INDEX 10000000
- long int ticks;
- time_t end_time;
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- void err_quit(char *msg);
- void idx_th(void *arg);
- void mon_th(void *arg);
- int main(int argc, char *argv[]) {
- pthread_t idx_th_id;
- pthread_t mon_th_id;
- int ret;
- end_time = time(NULL) 30; /* 30s */
- ret = pthread_create(&idx_th_id, NULL, (void*)idx_th, NULL);
- if (ret) err_quit("pthread_create; idx_th");
- ret = pthread_create(&mon_th_id, NULL, (void*)mon_th, NULL);
- if (ret) err_quit("pthread_create; mon_th");
- pthread_join(idx_th_id, NULL);
- pthread_join(mon_th_id, NULL);
- exit(EXIT_SUCCESS);
- }
- /* increase the ticks */
- void idx_th(void *arg) {
- long l;
- int ret;
- while (time(NULL) < end_time) {
- ret = pthread_mutex_lock(&mutex);
- if (ret) err_quit("pthread_mutex_lock");
- for (l = 0l; l < INDEX; l ) {
- ticks ;
- }
- ret = pthread_mutex_unlock(&mutex);
- if (ret) err_quit("pthread_mutex_unlock");
- sleep(1);
- }
- }
- /* monitor the ticks */
- void mon_th(void *arg) {
- int nolock = 0;
- int ret;
- while (time(NULL) < end_time) {
- sleep(3);
- ret = pthread_mutex_trylock(&mutex);
- if (ret != EBUSY) {
- if (ret) err_quit("pthread_mutex_trylock");
- printf("mon_th: got lock at %ld ticks\n", ticks);
- ret = pthread_mutex_unlock(&mutex);
- if (ret) err_quit("pthread_mutex_unlock");
- } else {
- nolock ;
- }
- }
- printf("mon_th missed lock %d times\n", nolock);
- }
- void err_quit(char *msg) {
- perror(msg);
- exit(EXIT_FAILURE);
- }
阅读(1239) | 评论(0) | 转发(0) |