Can using "register" make code non-reentrant?

Discussion in 'C++' started by Bob from Chesham Bois, Jul 20, 2009.

  1. [ Earlier incomplete post sent by mistake - sorry! ]

    Can using "register make C++ code non-reentrant? For example:

    int myIncrement ( int myinput)
    {
    register int newint = myinput;
    newint++;
    return newint;
    }

    If two threads enter this function concurrently, will they share the
    same hardware register for "newint"? In which case the second thread
    may overwrite the value that the first thread stored in the register.
    Is this a danger? Whereas if the "register" declaration is not used,
    there is no danger, because each thread has its own "newint" on its
    stack.
    Bob from Chesham Bois, Jul 20, 2009
    #1
    1. Advertising

  2. Bob from Chesham Bois <> writes:

    > [ Earlier incomplete post sent by mistake - sorry! ]
    >
    > Can using "register make C++ code non-reentrant? For example:
    >
    > int myIncrement ( int myinput)
    > {
    > register int newint = myinput;
    > newint++;
    > return newint;
    > }
    >
    > If two threads enter this function concurrently, will they share the
    > same hardware register for "newint"? In which case the second thread
    > may overwrite the value that the first thread stored in the register.
    > Is this a danger? Whereas if the "register" declaration is not used,
    > there is no danger, because each thread has its own "newint" on its
    > stack.


    No. Either the two threads are running in different cores, in which
    case each core has its own set of registers, or they're running in the
    same core, in which case they don't run at the same time, and the OS
    takes care of saving and restoring the registers so it looks like each
    thread has its own registers.

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Jul 20, 2009
    #2
    1. Advertising

  3. Bob from Chesham Bois

    Fred Zwarts Guest

    "Bob from Chesham Bois" <> wrote in message news:...
    >[ Earlier incomplete post sent by mistake - sorry! ]
    >
    > Can using "register make C++ code non-reentrant? For example:
    >
    > int myIncrement ( int myinput)
    > {
    > register int newint = myinput;
    > newint++;
    > return newint;
    > }
    >
    > If two threads enter this function concurrently, will they share the
    > same hardware register for "newint"? In which case the second thread
    > may overwrite the value that the first thread stored in the register.
    > Is this a danger? Whereas if the "register" declaration is not used,
    > there is no danger, because each thread has its own "newint" on its
    > stack.


    No. Even if register is not used the compiler may choose to use a register
    for auto variables.
    Fred Zwarts, Jul 20, 2009
    #3
  4. In message <>, Pascal J.
    Bourguignon <> writes
    >Bob from Chesham Bois <> writes:
    >
    >> [ Earlier incomplete post sent by mistake - sorry! ]
    >>
    >> Can using "register make C++ code non-reentrant? For example:
    >>
    >> int myIncrement ( int myinput)
    >> {
    >> register int newint = myinput;
    >> newint++;
    >> return newint;
    >> }
    >>
    >> If two threads enter this function concurrently, will they share the
    >> same hardware register for "newint"? In which case the second thread
    >> may overwrite the value that the first thread stored in the register.
    >> Is this a danger? Whereas if the "register" declaration is not used,
    >> there is no danger, because each thread has its own "newint" on its
    >> stack.

    >
    >No. Either the two threads are running in different cores, in which
    >case each core has its own set of registers, or they're running in the
    >same core, in which case they don't run at the same time, and the OS
    >takes care of saving and restoring the registers so it looks like each
    >thread has its own registers.
    >

    In any case, register is little more than a hint, so the compiler
    probably ignores it.

    --
    Richard Herring
    Richard Herring, Jul 20, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hanspeter Moessenboeck

    Compiler generator Coco/R: Reentrant version

    Hanspeter Moessenboeck, Apr 27, 2005, in forum: Java
    Replies:
    0
    Views:
    495
    Hanspeter Moessenboeck
    Apr 27, 2005
  2. lander
    Replies:
    4
    Views:
    414
    lander
    Mar 10, 2008
  3. SHP

    detect non reentrant function

    SHP, Apr 8, 2008, in forum: C Programming
    Replies:
    1
    Views:
    702
    Antoninus Twink
    Apr 8, 2008
  4. rahul

    Re: Reentrant code

    rahul, Jun 17, 2008, in forum: C Programming
    Replies:
    3
    Views:
    457
  5. Walter Roberson

    Re: Reentrant code

    Walter Roberson, Jun 17, 2008, in forum: C Programming
    Replies:
    2
    Views:
    582
    Walter Roberson
    Jun 17, 2008
Loading...

Share This Page