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

cnd_signal函数


概要:
#include <threads.h>
int cnd_signal(cnd_t *cond);

描述:

该函数解锁函数调用时一个因等待参数cond指向条件变量而阻塞的线程。如果调用该函数时,没有线程因等待参数cond指向条件变量而阻塞,函数将什么也不做,并返回thrd_success


参数:
cnd_t *cond

指向条件变量的指针。


返回值:

如果调用成功,函数返回thrd_success;如果请求不能得到满足,函数返回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 
/*函数cnd_signal范例*/

/*
** 该例子创建了4个新线程,
** 其中3个线程用于修改number的值,第4个线程处于等待状态;
** 如果number的值大于等于LIMIT,第4个线程输出number的值。
*/

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

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

#define LIMIT 100       //解锁因条件变量而阻塞的线程的条件。
#define THREADS 4       //线程数。

unsigned int number = 0;        //如果number≥LIMIT,解锁因条件变量而阻塞的线程。
mtx_t mutex;                    //互斥。
cnd_t conditionVariable;        //条件变量。

/*操作number的函数。*/
int funcProcess(void *arg)
{
    do
    {
        mtx_lock(&mutex);
        
        /*如果条件满足就解锁因条件变量而阻塞的线程。*/
        if(number >= LIMIT)
        {
            cnd_signal(&conditionVariable);
            mtx_unlock(&mutex);
            
            thrd_exit(0);
        }
        number += *((unsigned int *)arg);
        
        mtx_unlock(&mutex);
    }while(number < LIMIT);
}

/*等待number值满足条件的函数。*/
int funcWait(void *arg)
{
    mtx_lock(&mutex);
    while(number < LIMIT)
    {
        cnd_wait(&conditionVariable, &mutex);
    }
    puts("Receive condition signal.");
    printf("number = %u\n", number);
    mtx_unlock(&mutex);
    
    thrd_exit(0);
}

int main(void)
{
    thrd_t threadId[THREADS];       //线程标识符。
    unsigned int data[THREADS-1] = {3, 6, 9};
    
    /*初始化条件变量。*/
    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);
    }
    
    /*创建新线程。*/
    for(int i=0; i<THREADS-1; ++i)
    {
        if(thrd_create((threadId+i), funcProcess, (data+i)) != thrd_success)
        {
            perror("thrd_create");
            exit(EXIT_FAILURE);
        }
    }
    
    if(thrd_create((threadId+THREADS-1), funcWait, NULL) != 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;
}


输出:

Receive condition signal.

number = 102

注:使用Pelles C编译。

102并不是唯一输出值;此程序也可能输出105或者108


相关内容:
cnd_broadcast 解锁所有等待条件变量的线程的函数。
cnd_destroy 销毁条件变量的函数。
cnd_init 创建条件变量的函数。
cnd_timedwait 支持超时等待条件变量的函数。
cnd_wait 等待条件变量的函数。