线程函数和类型介绍

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值