本文主要介绍Linux线程的概念以及线程的模型,并介绍基于POSIX线程的接口。
N:1用户线程模型
1:1核心线程模型
N:M混合线程模型
pthread_create函数
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
pthread_join函数
int pthread_join(pthread_t thread, void **retval);
pthread_exit函数
void pthread_exit(void *value_ptr);
pthread_self函数
pthread_t pthread_self(void);
pthread_cancel
int pthread_cancel(pthread_t thread);
pthread_detach函数
int pthread_detach(pthread_t thread);
实例:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #define ERR_EXIT(m) \ do { \ perror(m); \ exit(EXIT_FAILURE); \ } while (0) void *thread_routine(void *arg) { int i; for (i=0; i<20; ++i) { printf("B"); fflush(stdout); usleep(20); if (i == 3) { pthread_exit("ABC"); } } return 0; } int main() { pthread_t tid; int ret; if ((ret = pthread_create(&tid, NULL, thread_routine, NULL)) != 0) { fprintf(stderr, "pthread_create: %s\n", strerror(ret)); exit(EXIT_FAILURE); } int i; for (i=0; i<20; ++i) { printf("A"); fflush(stdout); usleep(20); } void *value; if ((ret = pthread_join(tid, &value)) != 0) { fprintf(stderr, "pthread_join: %s\n", strerror(ret)); exit(EXIT_FAILURE); } printf(" msg = %s\n", (char *)value); return 0; }
初始化与销毁属性
int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *attr);
获取与设置分离属性
int pthread_attr_getdetachstate(const pthread_attr_t \*attr, int \*detachstate); int pthread_attr_setdetachstate(const pthread_attr_t *attr, int detachstate);
获取与设置栈大小
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); int pthread_attr_getstacksize(pthread_attr_t \*attr, size_t \*stacksize);
获取与设置栈溢出保护区大小
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize); int pthread_attr_getguardsize(pthread_attr_t *attr, size_t *guardsize);
获取与设置线程竞争范围
int pthread_attr_setscope(pthread_attr_t *attr, int scope); int pthread_attr_getscope(pthread_attr_t *attr, int *scope);
获取与设置调度策略
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
获取与设置继承的调度策略
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched); int pthread_attr_getinheritsched(pthread_attr_t *attr, int *inheritsched);
获取与设置调度参数
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param); int pthread_attr_getschedparam(pthread_attr_t *attr, struct sched_param *param);
获取与设置并发级别
int pthread_setconcurrency(int new_level); int pthread_getconcurrency(void);
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)); int pthread_key_delete(pthread_key_t key); void *pthread_getspecific(pthread_key_t key); int pthread_setspecific(pthread_key_t key, const void *value); int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); pthread_once_t once_control = PTHREAD_ONCE_INIT;
实例:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #define ERR_EXIT(m) \ do { \ perror(m); \ exit(EXIT_FAILURE); \ } while (0) typedef struct tsd { pthread_t tid; char *str; } tsd_t; pthread_key_t key_tsd; pthread_once_t once_control = PTHREAD_ONCE_INIT; void destroy_routine(void *value) { printf("destroy\n"); free(value); } void init_routine() { pthread_key_create(&key_tsd, destroy_routine); } void *thread_routine(void *arg) { pthread_once(&once_control, init_routine); tsd_t *value = (tsd_t *)malloc(sizeof(tsd_t)); tsd_t *value = (tsd_t *)malloc(sizeof(tsd_t)); value->tid = pthread_self(); value->str = (char *)arg; pthread_setspecific(key_tsd, value); printf("%s setspecific %p\n", (char *)arg, value); value = pthread_getspecific(key_tsd); printf("tid=0x%x str=%s\n", value->tid, value->str); sleep(2); value = pthread_getspecific(key_tsd); printf("tid=0x%x str=%s\n", value->tid, value->str); return NULL; } int main() { // pthread_key_create(&key_tsd, destroy_routine); pthread_t tid1; pthread_t tid2; int ret; if ((ret = pthread_create(&tid1, NULL, thread_routine, "thread1")) != 0) { fprintf(stderr, "pthread_create: %s\n", strerror(ret)); exit(EXIT_FAILURE); } if ((ret = pthread_create(&tid2, NULL, thread_routine, "thread2")) != 0) { fprintf(stderr, "pthread_create: %s\n", strerror(ret)); exit(EXIT_FAILURE); } pthread_join(tid1, NULL); pthread_join(tid1, NULL); pthread_key_delete(key_tsd); return 0; }