linux 线程接口函数

 pthread_t 在头文件/usr/include/bits/pthreadtypes.h中定义

typedef unsigned long int pthread_t;

编译多线程程序时,需要 libpthread.a, 所以要使用 -lpthread 选项。

创建线程函数

       #include <pthread.h>

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

线程结束的两种方式

线程退出函数:

       #include <pthread.h>

       void pthread_exit(void *retval);

等待线程结束函数:

       #include <pthread.h>

       int pthread_join(pthread_t thread, void **retval);

pthread_join() 函数是一个阻塞函数,调用该函数的函数会被阻塞,直到被等待的线程执行结束为止,当函数返回时,被等待的线程资源将被系统回收。

pthread_exit() 和 pthread_join() 是线程退出的2种结束方式。

线程属性

线程属性 pthread_attr_t

union pthread_attr_t                                                                                                                                       
{
  char __size[__SIZEOF_PTHREAD_ATTR_T];
  long int __align;
};
#ifndef __have_pthread_attr_t
typedef union pthread_attr_t pthread_attr_t;
# define __have_pthread_attr_t 1
#endif

线程属性的初始化和销毁函数

       #include <pthread.h>

       int pthread_attr_init(pthread_attr_t *attr);
       int pthread_attr_destroy(pthread_attr_t *attr);

线程属性不能直接设置, 要通过函数间接执行相关操作。线程的属性主要有:是否分离、是否绑定、优先级、堆栈大小、堆栈位置。默认的属性是非绑定、非分离、1M的堆栈大小与父进程相同的优先级。

互斥锁

pthread_mutex_t 类型为线程互斥锁类型

/* Initialize a mutex.  */
extern int pthread_mutex_init (pthread_mutex_t *__mutex,
                   const pthread_mutexattr_t *__mutexattr)
     __THROW __nonnull ((1));

/* Destroy a mutex.  */
extern int pthread_mutex_destroy (pthread_mutex_t *__mutex)
     __THROW __nonnull ((1));

/* Try locking a mutex.  */
extern int pthread_mutex_trylock (pthread_mutex_t *__mutex)
     __THROWNL __nonnull ((1));

/* Lock a mutex.  */
extern int pthread_mutex_lock (pthread_mutex_t *__mutex)
     __THROWNL __nonnull ((1));

/* Unlock a mutex.  */
extern int pthread_mutex_unlock (pthread_mutex_t *__mutex)
     __THROWNL __nonnull ((1));

也可以声明/设置带有属性的互斥锁,

/* Initialize mutex attribute object ATTR with default attributes
   (kind is PTHREAD_MUTEX_TIMED_NP).  */
extern int pthread_mutexattr_init (pthread_mutexattr_t *__attr);

/* Destroy mutex attribute object ATTR.  */                                                                                                                  
extern int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);

/* Set the process-shared flag of the mutex attribute ATTR.  */
extern int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,int __pshared);

条件变量

extern int pthread_cond_init (pthread_cond_t *__restrict __cond,                                                                                             
                  const pthread_condattr_t *__restrict __cond_attr);
extern int pthread_cond_destroy (pthread_cond_t *__cond);
/* Wake up one thread waiting for condition variable COND.  */
extern int pthread_cond_signal (pthread_cond_t *__cond);

/* Wake up all threads waiting for condition variables COND.  */
extern int pthread_cond_broadcast (pthread_cond_t *__cond);

/* Wait for condition variable COND to be signaled or broadcast.
   MUTEX is assumed to be locked before. */

extern int pthread_cond_wait (pthread_cond_t *__restrict __cond,
                  pthread_mutex_t *__restrict __mutex);

/* Wait for condition variable COND to be signaled or broadcast until
   ABSTIME.  MUTEX is assumed to be locked before.  ABSTIME is an
   absolute time specification; zero is the beginning of the epoch
   (00:00:00 GMT, January 1, 1970). */

extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
                   pthread_mutex_t *__restrict __mutex,
                   const struct timespec *__restrict __abstime);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 下的线接口是 POSIX 线程(pthread)库,它是一套标准的 API,定义了线程的创建、同步、通信等操作。下面绍一些常用的函数和操作: ### 线程创建 在 POSIX 线程库中,线程的创建使用 `pthread_create` 函数。它的原型如下: ```c int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); ``` 其中: - `thread`:指向线程标识符的指针。 - `attr`:指向线程属性的指针,如果为 `NULL`,表示使用默认属性。 - `start_routine`:线程的入口函数,它必须是一个指向函数的指针,且返回类型为 `void *`,接受一个 `void *` 类型的参数。 - `arg`:传递给线程入口函数的参数。 `pthread_create` 函数成功时返回 0,否则返回错误码。 ### 线程等待 线程等待使用 `pthread_join` 函数,它的原型如下: ```c int pthread_join(pthread_t thread, void **retval); ``` 其中: - `thread`:线程标识符。 - `retval`:指向线程返回值的指针,如果不需要返回值,可以传递 `NULL`。 `pthread_join` 函数会阻塞当前线程,直到指定的线程退出并返回。如果线程已经被分离,或者已经退出,`pthread_join` 函数会立即返回,并且不会修改 `retval` 指向的值。`pthread_join` 函数成功时返回 0,否则返回错误码。 ### 线程分离 线程分离使用 `pthread_detach` 函数,它的原型如下: ```c int pthread_detach(pthread_t thread); ``` 其中: - `thread`:线程标识符。 `pthread_detach` 函数将指定的线程标记为分离状态,它的资源会在线程退出时自动被回收,无法再次被等待。如果一个线程已经被分离,再次调用 `pthread_detach` 函数会返回错误码。`pthread_detach` 函数成功时返回 0,否则返回错误码。 ### 线程取消 线程取消使用 `pthread_cancel` 函数,它的原型如下: ```c int pthread_cancel(pthread_t thread); ``` 其中: - `thread`:线程标识符。 `pthread_cancel` 函数向指定的线程发送一个取消请求,如果线程允许取消,它将会退出。线程可以通过设置取消状态和取消类型来控制取消的行为。`pthread_cancel` 函数成功时返回 0,否则返回错误码。 ### 线程同步 线程同步使用互斥锁、条件变量等机制来实现。这些机制可以通过 pthread 库提供的函数来使用,如 `pthread_mutex_init`、`pthread_mutex_lock`、`pthread_cond_init`、`pthread_cond_wait` 等等。这些函数的使用方法比较复杂,需要根据具体的应用场景来选择和使用。 以上是一些常用的 POSIX 线程库的函数和操作,使用时需要根据具体的业务需求进行选择和使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值