Can using "register" make code non-reentrant?

  • Thread starter Bob from Chesham Bois
  • Start date
B

Bob from Chesham Bois

[ 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.
 
P

Pascal J. Bourguignon

Bob from Chesham Bois said:
[ 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.
 
F

Fred Zwarts

Bob from Chesham Bois said:
[ 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.
 
R

Richard Herring

Pascal J. said:
Bob from Chesham Bois said:
[ 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top