NO

Author Topic: Bug in atomic_is_lock_free?  (Read 1898 times)

Offline Werner

  • Member
  • *
  • Posts: 20
Bug in atomic_is_lock_free?
« on: May 01, 2023, 04:18:21 PM »
The standard states in 7.17.5.1 that atomic_is_lock_free has to be called with *a pointer to* the object in dispute.
So, except I am terribly mistaken, if the object is a pointer, one has to pass a pointer to a pointer.
But in the following test code one gets e. g.:

error #2141: Type error in argument 1 to 'atomic_is_lock_free'; '_Atomic float * *' is invalid.

Am I missing or misunderstanding something?

Code: [Select]
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    _Atomic float        var_float;
    _Atomic double       var_double;
    _Atomic long double  var_long_double;
    _Atomic float*       p_var_float;
    _Atomic double*      p_var_double;
    _Atomic long double* p_var_long_double;
   
    atomic_init(&var_float, 42.0);
    atomic_init(&var_double, 4711.1);
    atomic_init(&var_long_double, 1000000.12);
    atomic_init(&p_var_float, &var_float);
    atomic_init(&p_var_double, &var_double);
    atomic_init(&p_var_long_double, &var_long_double);

    printf("\"_Atomic float        var_float\"         is %slock-free.\n", atomic_is_lock_free(& var_float)        ? "" : "not ");
    printf("\"_Atomic double       var_double\"        is %slock-free.\n", atomic_is_lock_free(& var_double)       ? "" : "not ");
    printf("\"_Atomic long double  var_long_double\"   is %slock-free.\n", atomic_is_lock_free(& var_long_double)  ? "" : "not ");
    printf("\"_Atomic float*       p_var_float\"       is %slock-free.\n", atomic_is_lock_free(&p_var_float)       ? "" : "not ");
    printf("\"_Atomic double*      p_var_double\"      is %slock-free.\n", atomic_is_lock_free(&p_var_double)      ? "" : "not ");
    printf("\"_Atomic long double* p_var_long_double\" is %slock-free.\n", atomic_is_lock_free(&p_var_long_double) ? "" : "not ");

    exit(EXIT_SUCCESS);
}

Werner (on version 12.00)

« Last Edit: May 01, 2023, 04:19:59 PM by Werner »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Bug in atomic_is_lock_free?
« Reply #1 on: May 01, 2023, 04:45:47 PM »
As far as I understand, the pointed-to object must be atomic so float * _Atomic or _Atomic float * _Atomic but not _Atomic float *.

FWIW, a recent version of LLVM seems to agree about the errors (and IMHO the C11/C17/C2X standard could be a little clearer about atomic types).
/Pelle

Offline Werner

  • Member
  • *
  • Posts: 20
Re: Bug in atomic_is_lock_free?
« Reply #2 on: May 01, 2023, 06:55:26 PM »
Yeah, of course you are absolutely right! My mistake is blatant.
I should go back to 101 "Pointers for the Rookie" ...

I apologize for bothering you and the community with my silly mistakes.
Thanks for your immediate response.

Werner

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Bug in atomic_is_lock_free?
« Reply #3 on: May 01, 2023, 08:03:24 PM »
No problem! (I did pretty much the same mistake some years ago...)
/Pelle