C语言中如何编写线程函数?
- 内容介绍
- 文章标签
- 相关推荐
本文共计621个文字,预计阅读时间需要3分钟。
除了创建线程的 thread_create() 函数、获取返回值的 thread_join() 函数和释放线程占用资源的 thread_detach() 函数外,C11 还提供了5个用于线程控制的函数:thrd_t thrd_current(void);
除了创建线程的 thread_create()函数、获取返回值的 thread_join()函数和释放线程占用资源的 thread_detach()函数,C11 还提供了另外用于线程控制的 5 个函数:
thrd_t thrd_current(void);
该函数返回其所在线程的线程标识。
int thrd_equal(thrd_t thr0,thrd_t thr1);
仅当两个线程标识符 thr0、thr1 分别引用了两个不同线程时,返回 0。
int thrd_sleep(const struct timespec*duration,struct timespec*remaining);
使得正在调用的线程等待一段时间,等待时间由 duration 指定。仅当该函数收到唤醒的信号时,它才提前返回。在这种情况下,该函数将剩余倒数时间保留在 remaining 引用的对象中,假设 remaining 不是一个空指针。指针 duration 和 remaining 不得指向同一个对象。
结构参数 timespec 有两个成员,分别用于存储秒和纳秒:
time_t tv_sec; // 秒≥0
long tv_nsec; // 0 ≤纳秒≤999 999 999
结构中成员的顺序未被指定。
本文共计621个文字,预计阅读时间需要3分钟。
除了创建线程的 thread_create() 函数、获取返回值的 thread_join() 函数和释放线程占用资源的 thread_detach() 函数外,C11 还提供了5个用于线程控制的函数:thrd_t thrd_current(void);
除了创建线程的 thread_create()函数、获取返回值的 thread_join()函数和释放线程占用资源的 thread_detach()函数,C11 还提供了另外用于线程控制的 5 个函数:
thrd_t thrd_current(void);
该函数返回其所在线程的线程标识。
int thrd_equal(thrd_t thr0,thrd_t thr1);
仅当两个线程标识符 thr0、thr1 分别引用了两个不同线程时,返回 0。
int thrd_sleep(const struct timespec*duration,struct timespec*remaining);
使得正在调用的线程等待一段时间,等待时间由 duration 指定。仅当该函数收到唤醒的信号时,它才提前返回。在这种情况下,该函数将剩余倒数时间保留在 remaining 引用的对象中,假设 remaining 不是一个空指针。指针 duration 和 remaining 不得指向同一个对象。
结构参数 timespec 有两个成员,分别用于存储秒和纳秒:
time_t tv_sec; // 秒≥0
long tv_nsec; // 0 ≤纳秒≤999 999 999
结构中成员的顺序未被指定。

