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

mtx_timed枚举常量


概要:
#include <threads.h>
enum {
    mtx_plain = value,		//value值由实现定义。
    mtx_recursive = value,	//value值由实现定义。
    mtx_timed = value		//value值由实现定义。
 };

描述:

该枚举常量用作mtx_init函数的参数,创建支持超时的互斥对象。


在Pelles C编译器<threads.h>头文件中,互斥类型定义如下:

    enum {
        mtx_plain = 0x01,   
        mtx_timed = 0x02,     
        mtx_recursive = 0x10   
     };

范例:
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 
/*枚举常量mtx_timed范例*/

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

#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
 
#define TIMEOUT 5  //超时时间5秒。

mtx_t mutex;

/*尝试锁定互斥锁的子线程函数。*/
int func(void *arg)
{
    struct timespec ts;

    /*计算绝对超时时间。*/
    timespec_get(&ts, TIME_UTC);
    ts.tv_sec += TIMEOUT;

    printf("Child thread: Attempt to lock the mutex and wait for %d seconds.\n", TIMEOUT);

    int ret = mtx_timedlock(&mutex, &ts);

    if(ret==thrd_timedout)
    {
        puts("Child thread: The time specified was reached without acquiring the requested resource.");

        return thrd_timedout;
    }
    else if(ret==thrd_error)
    {
        puts("Child thread: The request to acquire the mutex lock could not be honored.");

        return thrd_error;
    }
    else
    {
        puts("Child thread: Successfully acquired the mutex lock.The data is being processed...");
        mtx_unlock(&mutex);
        puts("Child thread: Released the mutex.");

        return 0;
    }
}

int main(void)
{
    thrd_t threadId;

    /*初始化支持超时的互斥。*/
    if(mtx_init(&mutex, mtx_timed) != thrd_success)
    {
        fprintf(stderr, "The request to create a mutex object could not be honored.");

        exit(EXIT_FAILURE);
    }

    /*主线程先锁定互斥。*/
    if(mtx_lock(&mutex) != thrd_success)
    {
        fprintf(stderr, "Failed to lock mutex.\n");
        mtx_destroy(&mutex);

        exit(EXIT_FAILURE);
    }
    puts("Main thread: Acquired the mutex.");

    /*创建子线程。*/
    if(thrd_create(&threadId, func, NULL) != thrd_success)
    {
        perror("thrd_create");

        exit(EXIT_FAILURE);
    }

    /*主线程2秒后释放互斥。*/
    puts("Main thread: The mutex will be released in 2 seconds.");
    thrd_sleep(&(struct timespec){.tv_sec = 2}, NULL);
    mtx_unlock(&mutex);
    puts("Main thread: Released the mutex.");

    /*连接线程。*/
    thrd_join(threadId, NULL);

    /*销毁互斥。*/
    mtx_destroy(&mutex);

    return 0;
}


输出:

Main thread: Acquired the mutex.

Main thread: The mutex will be released in 2 seconds.

Child thread: Attempt to lock the mutex and wait for 5 seconds.

Main thread: Released the mutex.

Child thread: Successfully acquired the mutex lock.The data is being processed...

Child thread: Released the mutex.


相关内容:
mtx_recursive 表示递归互斥的枚举常量。
mtx_plain 表示简单、非递归、不支持超时互斥的枚举常量。


主要参考资料:

1、ISO/IEC 9899:2024

2、ISO/IEC 9899:2018