Q: Optimization issues?

From: Jakob Bieling (netsurf_at_gmy.net)
Date: 10/24/03


Date: Fri, 24 Oct 2003 16:36:06 +0200

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!

--
jb
(replace y with x if you want to reply by e-mail)