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

mtx_recursive枚举常量


概要:
#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 
95 
96 
97 
98 
99 
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*枚举常量mtx_recursive范例*/

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

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

#define NUMBER 5

mtx_t mutex;
int shared_sum = 0; //所有线程累积的和。

/*二叉树节点结构。*/
typedef struct node{
    int number;
    struct node *left;
    struct node *right;
} Node;

/*创建二叉树新节点函数。*/
Node *create_node(int number)
{
    Node *new_node = (Node *)malloc(sizeof(Node));

    if(new_node==NULL)
    {
        fprintf(stderr, "Memory allocation failed.\n");

        return NULL;
    }
    else
    {
        new_node->number = number;
        new_node->left = NULL;
        new_node->right = NULL;

        return new_node;
    }
}

/*创建二叉树函数。*/
Node *create_binary_tree(const int *ptr)
{
    Node *root = create_node(ptr[0]);
    root->left = create_node(ptr[1]);
    root->right = create_node(ptr[2]);
    root->left->left = create_node(ptr[3]);
    root->left->right = create_node(ptr[4]);
    root->right->left = create_node(ptr[5]);
    root->right->right = create_node(ptr[6]);

    return root;
}

/*递归遍历二叉树函数。*/
void traverse_binary_tree(Node *root, int depth)
{
    if(root==NULL)
        return;

    mtx_lock(&mutex);

    shared_sum += root->number;
    printf("Thread %lX: node %2d depth %d sum %3d\n",    \
           thrd_current(), root->number, depth, shared_sum);

    /*递归遍历子树。*/
    traverse_binary_tree(root->left, depth+1);
    traverse_binary_tree(root->right, depth+1);

    mtx_unlock(&mutex);
}

/*释放二叉树函数。*/
void free_binary_tree(Node *root)
{
    if(root==NULL)
        return;

    free_binary_tree(root->left);
    free_binary_tree(root->right);
    free(root);
}

/*新线程函数。*/
int thread_func(void *arg)
{
    traverse_binary_tree((Node *)arg,0);

    return 0;
}

int main(void)
{
    thrd_t thread1, thread2;
    const int arr1[] = {3,6,9,12,15,18,21};
    const int arr2[] = {2,4,6,8,10,12,14};

    /*创建二叉树。*/
    Node *tree1 = create_binary_tree(arr1);
    Node *tree2 = create_binary_tree(arr2);

    /*初始化递归锁。*/
    if(mtx_init(&mutex, mtx_plain | mtx_recursive) != thrd_success)
    {
        fprintf(stderr, "mtx_init failed.\n");
        exit(EXIT_FAILURE);
    }

    /*创建线程遍历二叉树。*/
    if(thrd_create(&thread1, thread_func, tree1) != thrd_success)
    {
        fprintf(stderr, "thrd_create for thread1 failed.\n");
        exit(EXIT_FAILURE);
    }

    if(thrd_create(&thread2, thread_func, tree2) != thrd_success)
    {
        fprintf(stderr, "thrd_create for thread2 failed.\n");
        exit(EXIT_FAILURE);
    }

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

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

    /*释放二叉树内存。*/
    free_binary_tree(tree1);
    free_binary_tree(tree2);

    printf("Total sum: %d\n", shared_sum);

    return 0;
}


输出:

Thread: A000006D0: node 03 depth 0 sum 003

Thread: A000006D0: node 06 depth 1 sum 009

Thread: A000006D0: node 12 depth 2 sum 021

Thread: A000006D0: node 15 depth 2 sum 036

Thread: A000006D0: node 09 depth 1 sum 045

Thread: A000006D0: node 18 depth 2 sum 063

Thread: A000006D0: node 21 depth 2 sum 084

Thread: A000007F0: node 02 depth 0 sum 0086

Thread: A000007F0: node 04 depth 1 sum 090

Thread: A000007F0: node 08 depth 2 sum 098

Thread: A000007F0: node 10 depth 2 sum 108

Thread: A000007F0: node 06 depth 1 sum 114

Thread: A000007F0: node 12 depth 2 sum 126

Thread: A000007F0: node 14 depth 2 sum 140

Total sum: 140


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


主要参考资料:

1、ISO/IEC 9899:2024

2、ISO/IEC 9899:2018