473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Q: Optimization issues?

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)
Jul 19 '05 #1
3 1238

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bn*************@news.t-online.com...
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
Jul 19 '05 #2
Jakob Bieling wrote:
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.

Jul 19 '05 #3
> 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.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim | last post by:
Hello, Since this is a newsgroup about php & web related topics, I wanted to share a new Search Engine Optimization tool that we just released. I know many of you have non optimized dynamic...
5
by: Steven T. Hatton | last post by:
In the following code I have attempted to perform all calculations that are repeated in the mathematical expression for the rotation being evaluated. IOW, I calculate cos(_angle) only once, and put...
4
by: Neil | last post by:
I just resolved a strange situation I was having with an ODBC linked SQL 7 view in an Access 2000 MDB file, and I'm trying to get some understanding as to what happened. The linked view was...
9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
3
by: John Welch | last post by:
I'm developing my first multi-user application, and have been reading a lot about the basics of multi-user optimization (ADH vol 2, this newsgroup's archives, etc.). I've decided to go with a file...
7
by: Rajeev | last post by:
Hello, I'm using gcc 3.4.2 on a Xeon (P4) platform, all kinds of speed optimizations turned on. For the following loop R=(evaluate here); // float N=(evaluate here); // N min=1 max=100...
65
by: He Shiming | last post by:
Hi, I just wrote a function that has over 200 "cases" wrapped in a "switch" statement. I'm wondering if there are performance issues in such implementation. Do I need to optimize it some way? ...
7
by: Dale | last post by:
A year or two ago, I read an article on Microsoft's MSDN or Patterns and Practices site about application optimization when using strings. Some of the recommendations were: use string.Empty...
4
by: specialdosa | last post by:
We use DB2-LUW-8.2. I am doing some research on performance issues and I read about the "OPTIMIZATION LEVELS' and their impacts on Joins. How do I get the Optimization level implemented here?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.