当前位置: C语言 -- 标准库 -- <threads.h> -- cnd_timedwait

cnd_timedwait函数


概要:
#include <threads.h>
int cnd_timedwait(cnd_t *restrict cond, mtx_t *restrict mtx,
      const struct timespec *restrict ts);

描述:

该函数会自动解锁参数mtx指向的互斥,并阻塞调用线程,直至调用cnd_signal或者cnd_broadcast函数唤醒参数cond指向的条件变量,或者到达参数ts指定的超时时间(该时间为基于TIME_UTC的日历时间。),或者直至某种不明原因解除阻塞。当调用线程解除阻塞时,调用线程在函数返回前再次加锁参数mtx指向的互斥。调用cnd_timedwait函数前,调用线程应加锁参数mtx指向的互斥。


参数:
cnd_t *cond

指向条件变量的指针。

mtx_t *mtx

指向互斥的指针。

const struct timespec *restrict ts

指向struct timespec类型对象的指针,该对象指定等待条件变量的时间。


返回值:

如果调用成功,函数返回thrd_success;如果到达参数ts指定的时间,还没有获得请求的资源,函数返回thrd_timedout;如果请求无法得到满足,函数返回thrd_error


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
/*函数cnd_timedwait范例*/

/*
** 该例子创建了4个新线程,
** 其中1个线程用于数据处理,另外3个线程处于等待状态;
** 如果state的值为true,将唤醒所有等待线程。
*/

#ifdef __STDC_NO_THREADS__
#error "Implementation does not support multi-threads."
#endif

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>

#define THREADS 4       //线程数。
#define TIME 5          //等待时间,单位:秒。

mtx_t mutex;                //互斥。
cnd_t conditionVariable;    //条件变量。
bool state = false;

/*数据处理函数。*/
int funcProcess(void *arg)
{
    mtx_lock(&mutex);
    
    /*数据处理后,将state的值设置为true。*/
    state = true;
    
    /*如果条件满足就解锁因条件变量而阻塞的线程。*/
    if(state == true)
    {
        cnd_broadcast(&conditionVariable);
    }
    
    mtx_unlock(&mutex);
    thrd_exit(0);
}

/*等待state值满足条件的函数。*/
int funcWait(void *arg)
{
    mtx_lock(&mutex);
    while(state == false)
    {
        switch(cnd_timedwait(&conditionVariable, &mutex, &(struct timespec){TIME, 0}))
        {
        case thrd_timedout:
            puts("The specified waiting time has elapsed.");
            thrd_exit(thrd_timedout);
        case thrd_error:
            perror("thrd_error");
            thrd_exit(thrd_error);
        default:
            break;
        }
     }
    printf("Thread %d received condition signal.\n", *((int *)arg));
    mtx_unlock(&mutex);
    
    thrd_exit(0);
}

int main(void)
{
    thrd_t threadId[THREADS];       //线程标识符。
    int data[THREADS-1];
    
    /*初始化条件变量。*/
    if(cnd_init(&conditionVariable) != thrd_success)
    {
        perror("cnd_init");
        exit(EXIT_FAILURE);
    }
    
    /*初始化互斥。*/
    if(mtx_init(&mutex, mtx_plain) != thrd_success)
    {
        perror("mtx_init");
        exit(EXIT_FAILURE);
    }
    
    /*创建新线程。*/
    if(thrd_create(threadId, funcProcess, NULL) != thrd_success)
    {
        perror("thrd_create");
        exit(EXIT_FAILURE);
    }
    
    for(int i=0; i<THREADS - 1; ++i)
    {
        data[i] = i + 1;
        if(thrd_create((threadId+i+1), funcWait, (data+i)) != thrd_success)
        {
            perror("thrd_create");
            exit(EXIT_FAILURE);
        }
    }
    
    /*连接线程。*/
    for(int i=0; i<THREADS; ++i)
    {
        thrd_join(threadId[i], NULL);
    }
    
    /*销毁条件变量和互斥。*/
    cnd_destroy(&conditionVariable);
    mtx_destroy(&mutex);
    
    return 0;
}


输出:

Thread 1 received condition signal.

Thread 2 received condition signal.

Thread 3 received condition signal.

注:使用Pelles C编译。


相关内容:
cnd_signal 解锁一个等待条件变量的线程的函数。
cnd_destroy 销毁条件变量的函数。
cnd_init 创建条件变量的函数。
cnd_broadcast 解锁所有等待条件变量的线程的函数。
cnd_wait 等待条件变量的函数。