Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can using "register" make code non-reentrant?

Reply
Thread Tools

Can using "register" make code non-reentrant?

 
 
Bob from Chesham Bois
Guest
Posts: n/a
 
      07-20-2009
[ 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.

 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      07-20-2009
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__
 
Reply With Quote
 
 
 
 
Fred Zwarts
Guest
Posts: n/a
 
      07-20-2009

"Bob from Chesham Bois" <> wrote in message news:89473edf-fca7-4888-87c2-...
>[ 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.
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      07-20-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.9 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles? PerlFAQ Server Perl Misc 0 01-12-2011 11:00 PM
Freeware to make animated GIF (can Irfanview make an animated GIF?) Annu Pai Digital Photography 4 11-23-2009 12:52 AM
how to make corresponding java code for html code david wolf Java 1 04-21-2006 09:58 PM
Using a photo-editor to make web pages - how can I reduce the source code? Joey HTML 15 01-10-2006 09:51 AM
how can i write C code that generate random number without using built in code Sweety C Programming 6 04-04-2004 11:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57