Q: Optimization issues?

J

Jakob Bieling

Hi,

I have a question regarding optimization of code that uses
synchronization.

template <typename T>
T somefunc ()
{
enter_mutex ();
T t = shared_variable;
leave_mutex ();
return t;
}

Is the compiler allowed to optimize it, so that the code looks sort of
as if it was:

template <typename T>
T somefunc ()
{
enter_mutex ();
leave_mutex ();
return shared_variable;
}

? This is important, since 'shared_variable' must only be accessed after
'enter_mutex' but before 'leave_mutex' is called. Can this be a problem or
do I not have to worry?

thanks!
 
A

Antonio

Jakob Bieling said:
Hi,

I have a question regarding optimization of code that uses
synchronization.

template <typename T>
T somefunc ()
{
enter_mutex ();
T t = shared_variable;
leave_mutex ();
return t;
}

Replace T t = shared_variable with volatile T t ...
and try.

Antonio
 
S

stephan beal

Jakob said:
template <typename T>
T somefunc ()
{
enter_mutex ();
T t = shared_variable;
leave_mutex ();
return t;
}

Is the compiler allowed to optimize it, so that the code looks sort of
as if it was:

template <typename T>
T somefunc ()
{
enter_mutex ();
leave_mutex ();
return shared_variable;
}

The compiler cannot blindly optimize away 'T t = ...' because that line does
*something* and the compiler doesn't really know what it does. As moving
removing it would definately change the meaning of the code the compiler,
at least as far as i understand, is not allowed to change it in this way
(and indeed should not, as the compiler should never change the *meaning*
of your source).


--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.
 
W

wogston

I have a question regarding optimization of code that uses
synchronization.

template <typename T>
T somefunc ()
{
enter_mutex ();
T t = shared_variable;
leave_mutex ();
return t;
}

Is the compiler allowed to optimize it, so that the code looks sort of
as if it was:

template <typename T>
T somefunc ()
{
enter_mutex ();
leave_mutex ();
return shared_variable;
}

? This is important, since 'shared_variable' must only be accessed after
'enter_mutex' but before 'leave_mutex' is called. Can this be a problem or
do I not have to worry?

Compiler doesn't have to resort to the "optimization" you present here if it
implements NRVO, it will write the shared_variable directly to the return
value slot. If no NRVO, worst case is little extra copying stuff around.. if
compiler does the "optimization" you ask if it would do, the compiler would
be broken and not usable for threading.
 

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
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top