Initialization of the atomic flag in the malloc'd structure

I try to use atomics in Cin FreeBSD 10.1 Release with clang 3.6.1, however, when I try to compile a program using the ATOMIC_FLAG_INITin variable atomic_flagin struct, I get error: expected expression.

Here is the program I'm trying to compile.

#include <stdio.h>
#include <stdatomic.h>

struct map
{

    atomic_flag flag;

};

int main(void)
{
    struct map *m = malloc(sizeof(struct map));

    m->flag = ATOMIC_FLAG_INIT;

    free(m);

    return 0;
}

I can use atomic_flagout structs, as in the example below, but not in structs, since you use atomic variables in C structs?

#include <stdio.h>
#include <stdatomic.h>

int main(void)
{
    atomic_flag flag = ATOMIC_FLAG_INIT;

    return 0;
}
+4
source share
2 answers

atomic_flag it doesn’t matter which you can assign or read, but only the internal state.

atomic_flag - ( , _explicit), . , malloc, " " (7.17.8 p4 C11). , , atomic_flag_clear, "clear", atomic_flag_test_and_set, "set".

atomic_flag_clear malloc, ATOMIC_FLAG_INIT.

+5

atomic_flag (ATOMIC_FLAG_INIT) - undefined, , , .

, , .

-1

Source: https://habr.com/ru/post/1598769/


All Articles