Linux编程常用的函数(二) 线程

(二)线程


1. #include<thread.h> int pthread_equal(pthread_t tid1, pthread_t tid2);
//相等返回非0,否则返回0
2. pthread_t pthread_self(void);返回调用线程的ID
3. int pthread_create(pthread_t *restrict tidp,
  const pthread_attr_t *restrict attr, void *(*start_rtn)(void), void *restrict arg) ;
创建线程:成功返回0,否则返回错误编号
4. void pthread_exit(void *rval_ptr);//终止线程
5. int pthread_join(pthread_t thread, void **rval_ptr);
//自动线程置于分离状态,以恢复资源。成功返回0,否则返回错误编号
6. int pthread_cancel(pthread_t tid);
//请求取消同一进程中的其他线程;成功返回0,否则返回错误编号
7. void pthread_cleanup_push(void (*rtn)(void *), void *arg);
    //建立线程清理处理程序
8. void pthread_cleanup_pop(int execute);//调用建立的清理处理程序
9. int pthread_detach(pthread_t tid);//使线程进入分离状态,已分离也不出错
10.int pthread_mutex_init(pthread_mutex_t *restrict mutex,
  const pthread_nutexattr_t *restrict attr)//初始化互斥量;成功0,失败返回错误编号
11.int pthread_mutex_destroy(pthread_mutex_t *mutex);
//若有调用malloc动态分配内存则用该函数释放;成功0,失败返回错误编号
12.int pthread_mutex_lock(pthread_mutex_t *mutex);//锁住互斥量
  int pthread_mutex_trylock(pthread_mutex_t *mutex);//尝试上锁
  int pthread_mutex_unlock(pthread_mutex_t *mutex);//解锁
  成功返回0,否则返回错误编号
13.int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr)//初始化读写锁
  int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);//释放资源,在释放内存之前使用
成功返回0,否则返回错误编号
14.int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);//在读模式下锁定读写锁
  int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);//在写模式下锁定读写锁
  int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);//锁住读写锁
15.int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);//尝试在读模式下锁定读写锁
  int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);//尝试在写模式下锁定读写锁
成功返回0,否则返回错误编号
16.int pthread_cond_init(pthread_cond_t *restrict cond, pthread_condattr_t * restrict attr)
//初始化条件变量
  int pthread_cond_destroy(pthread_cond_t *cond);//去除初始化条件变量
成功返回0,否则返回错误编号
17.int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex)
  int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex,
  const struct timespec *restrict timeout);
  //等待条件变为真,如果在给定的时间内条件不能满足,那么会生成一个代表出错码的返回变量 ;成功返回0,错误返回错误编号
18.int pthread_cond_signal(pthread_cond_t *cond);//唤醒等待该条件的某个线程
  int pthread_cond_broadcast(pthread_cond_t *cond)//唤醒等待该条件的所有线程
19.int pthread_attr_init(pthread_attr_t *attr);//初始化线程属性
  int pthread_attr_destroy(pthread_attr_t *attr);//释放内存空间(动态分配时调用)
成功返回0,否则返回错误编号
20.int pthread_attr_getdetachstate(const pthread_attr_t *restrict attr, int *detachstate);
//获取线程的分离状态
  int pthread_attr_setdetachstate(const pthread_attr_t *restrict attr, int detachstate);
  //设置分离状态 PTHREAD_CREATE_DETACHED:以分离状态启动线程
  PTHREAD_CREATE_JOINABLE:正常启动线程,应用程序可以获取线程的终止状态
    成功返回0,否则返回错误编号
21.int pthread_attr_getstack(const pthread_attr_t *restrict attr,void **restrict stackaddr, size_t *restrict stacksize);//获取线程的栈位置
int pthread_attr_setstack(const pthread_attr_t *attr, void *stackaddr, size_t *stacksize)
//设置新建线程的栈位置 ;成功返回0,否则返回错误编号
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值