1. pthread_t
表示线程 ID
typedef unsigned long int pthread_t
2. pthread_attr_t
线程属性结构信息
typedef struct
{
int detachstate; 线程的分离状态
int schedpolicy; 线程调度策略
struct sched_param schedparam; 线程的调度参数
int inheritsched; 线程的继承性
int scope; 线程的作用域
size_t guardsize; 线程栈末尾的警戒缓冲区大小
int stackaddr_set;
void * stackaddr; 线程栈的位置
size_t stacksize; 线程栈的大小
}pthread_attr_t;
用 pthread_attr_init 函数初始化这个结构
用 pthread_attr_destory 给这个结构去除初始化
用 pthread_set_detachstate 对这个属性设置线程分离状态
下面是这三个函数的声明和注释:
/* Thread attribute handling. */
/* Initialize thread attribute *ATTR with default attributes
(detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
no user-provided stack). */
extern int pthread_attr_init (pthread_attr_t *__attr) __THROW __nonnull ((1));
/* Destroy thread attribute *ATTR. */
extern int pthread_attr_destroy (pthread_attr_t *__attr)
__THROW __nonnull ((1));
/* Get detach state attribute. */
extern int pthread_attr_getdetachstate (__const pthread_attr_t *__attr,
int *__detachstate)
__THROW __nonnull ((1, 2));
/* Set detach state attribute. */
extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,
int __detachstate)
__THROW __nonnull ((1));
links: http://blog.chinaunix.net/space.php?uid=9532036&do=blog&cuid=341886
3. pthread_mutex_t
线程互斥锁
pthread_mutex_lock 加锁
pthread_mutex_unlock 解锁
4. pthread_create
创建一个线程
#include <pthread.h>
/* Create a new thread, starting with execution of START-ROUTINE
getting passed ARG. Creation attributed come from ATTR. The new
handle is stored in *NEWTHREAD. */
extern int pthread_create (pthread_t *__restrict __newthread,
__const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROW __nonnull ((1, 3));
Returns: 0 if OK, error number on failure
4. 编译链接:
程序需要链接线程库
gcc -o target -lpthread xxx.c