Atomic operations and Thread safe code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • blackstreetcat@hotmail.com

    #1

    Atomic operations and Thread safe code

    consider this code :

    int i; //gobal var

    Thread1:
    i=some value;

    Thread2:
    if (i==2) dosomething();
    else dosomethingelse ();

    I want to write it to be thread safe without using synchronization
    objects.

    my questions are:

    1) is an assignment operation of an int atomic in c/c++?
    if so will thaat code work?

    volatile int i;

    int getI()
    { return i;//assignment is atomic so assignment to return
    value will be atomic also
    }

    void setI(int somevalue)
    {
    i=somevalue;//assignment atomic
    }

    Thread1:
    setI(somevalue) ;

    Thread2:
    if (getI()==2) dosomething();
    else dosomethingelse ();


    2) what operations in c are atomic?
    are mailslots pipes etc operations atomic/threadsafe ?


    thanks.

    Katy.

Working...