sig_atomic_t

This is a discussion on sig_atomic_t within the C++ Programming forums, part of the General Programming Boards category; i have couple of questions about sig_atomic_t 1. if the first declaration variable of sig_atomic_t is 0 (without assigning the ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    17

    sig_atomic_t

    i have couple of questions about sig_atomic_t
    1. if the first declaration variable of sig_atomic_t is 0 (without assigning the 0 value, just declare like sig_atomic_t atomic; )?
    2. suppose i have this code
    Code:
    sig_atmoic_t atomic;
    if( ++atomic == 1 ){
    }
    will this become 2 seperated atomic operation that first to ++ the atomic value, and then check if the value is 1?
    or it will be as one atomic operation that will guaranted that atomic variable inside if statement will not be read/write in another thread?

    thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    1. Variables of type sig_atomic_t are typically declared with static scope. That means it will be zero initialized by the system if it isn't explicitly initialized. For their intended use in signal handlers, they should also be declared as volatile.

    2. That's at least 2 atomic operations.

    >> ...will not be read/write in another thread?
    Thread? You mean signal handler?
    sig_atomic_t and threads have nothing to do with each other.

    gg

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    17
    >> ...will not be read/write in another thread?
    Thread? You mean signal handler?
    sig_atomic_t and threads have nothing to do with each other.
    i mean if there's two thread using the sig_atomic_t for synchronization instead of mutex.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    The only guarantees you have with sig_atomic_t have to do with signals. All bets are off for threads. For proper synchronization, use the primitives provided by the threading API.

    The new C++ standard will have atomic types for use by threads.

    gg

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21