Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

405
Visualizações
atomic_init is not thread safe in C, so why does it exist?

The C17 standard, section 7.17.2.2, states the below regardingvoid atomic_init(volatile A *obj, C value) defined in <stdatomic.h>:

Although this function initializes an atomic object, it does not avoid data races; concurrent access to the variable being initialized, even via an atomic operation, constitutes a data race.

Since the whole point of having atomic objects and atomic operations is to avoid data races, why does the atomic_init function exist? For example, why not do the below?

_Atomic int x = 7; 

Instead of:

_Atomic int x; 
atomic_init(&x, 7);

Also, why is it called atomic_init when it really is an assignment?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Atomic values may have hidden fields used to achieve the desired semantics, and those need to be initialized somehow. This can be achieved using an C variable initializer or using atomic_init.

For an static or automatic variable, that provides two options:

_Atomic int x = 7;
_Atomic int x; 
atomic_init( &x, 7 );

But that only leaves one option for dynamically-allocated variables.

_Atomic int *p = malloc( sizeof( _Atomic int ) );
atomic_init( p, 7 );
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda