Click here to Skip to main content
15,917,481 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralShading Pin
Gary Kirkham8-Aug-03 3:50
Gary Kirkham8-Aug-03 3:50 
GeneralRe: Shading Pin
Ryan Binns8-Aug-03 4:43
Ryan Binns8-Aug-03 4:43 
GeneralAutomate word and excel Pin
Jason Truong8-Aug-03 3:46
Jason Truong8-Aug-03 3:46 
GeneralRe: Automate word and excel Pin
David Crow8-Aug-03 5:35
David Crow8-Aug-03 5:35 
GeneralClickety Police !! Pin
Rage8-Aug-03 6:15
professionalRage8-Aug-03 6:15 
GeneralVS.NET/Dialog resize feature. Pin
Mike Doner8-Aug-03 3:38
Mike Doner8-Aug-03 3:38 
GeneralRe: VS.NET/Dialog resize feature. Pin
Joel Lucsy10-Aug-03 7:03
Joel Lucsy10-Aug-03 7:03 
GeneralCritical Sections Pin
Patje8-Aug-03 3:33
Patje8-Aug-03 3:33 
Today I profiled [using Rational Quantify] one of our applications in which a certain action took more and more time. I found the reason of the problem, but also noticed something else very strange.
Although the application runs most of the time in single-threaded mode, it has some actions that run multi-threaded. Therefore, some important datastructures were protected with a CriticalSection.
I noticed that the CriticalSection functions take about 15% of the total CPU time of the application. But there's more.

Consider the following small application:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

CRITICAL_SECTION Cs;

const long MAX_LOOPS=10000;

main ()
{
InitializeCriticalSection (&Cs);

for (long i=0;i<MAX_LOOPS;i++)
   {
   EnterCriticalSection (&Cs);
   Sleep (1);
   LeaveCriticalSection (&Cs);
   Sleep (1);
   }

DeleteCriticalSection (&Cs);
}


If this program is run through Quantify, it shows the following information:

Function                Calls    F Time  F+D Time   F(%)  F+D(%)     Avg F     Min F     Max F
mainCRTStartup              1  10617888  12589139  84,34  100,00  10617888  10617888  10617888
.Root.                      0         0  12589139   0,00  100,00         0         0         0
.main_0.                    0         0  12589139   0,00  100,00         0         0         0
LeaveCriticalSection    10000   1265606   1265606  10,05   10,05       126        71      1186
ExitProcess                 1    378351    378351   3,01    3,01    378351    378351    378351
GetModuleHandleA            2    103729    103729   0,82    0,82     51864      1617    102112
GetVersionExA               1     69788     69788   0,55    0,55     69788     69788     69788
GetFileType                 3     52111     52111   0,41    0,41     17370     11315     29385
HeapAlloc                  40     21896     21896   0,17    0,17       547       239      7388
HeapCreate                  1     17844     17844   0,14    0,14     17844     17844     17844


The function LeaveCriticalSection is indeed called 10000 times, but according to this table EnterCriticalSection is nowhere called in the application. There is no trace of the EnterCriticalSection function in the Rational Quantify reports.
It seemed like the compiler optimized EnterCriticalSection away, even if I compile with /Od /Zi. Can anybody explain this?
The second strange thing is that the maximum function time (Max F) of LeaveCriticalSection is much and much larger than the minimum function time (Min F). This means that in lot of cases the function was quite fast, but in some calls, the function was much slower. Can anybody explain this?

Of course simply removing the critical sections makes the application about 10-15% faster, but introduces a big risk when the application performs a multi-threaded action.
Nevertheless I thought that CriticalSections were the fastest way of synchronizing threads.

Does anybody know a method of improving the performance of CriticalSections or can anybody explain why LeaveCriticalSection takes much more time than EnterCriticalSection?

Thanks in advance.

Enjoy life, this is not a rehearsal !!!

My Articles:
- Implementing a Subject/Observer pattern with templates
- Different ways of writing class factories
- AutoRunner: a template class to automatically run start- and cleanup-code in code blocks
</

GeneralRe: Critical Sections Pin
Neville Franks8-Aug-03 11:21
Neville Franks8-Aug-03 11:21 
QuestionCustomization directory path per project ? Pin
yarp8-Aug-03 3:11
yarp8-Aug-03 3:11 
AnswerRe: Customization directory path per project ? Pin
keegan8-Aug-03 4:17
keegan8-Aug-03 4:17 
AnswerRe: Customization directory path per project ? Pin
HPSI8-Aug-03 4:56
HPSI8-Aug-03 4:56 
GeneralRe: Customization directory path per project ? Pin
yarp8-Aug-03 5:36
yarp8-Aug-03 5:36 
GeneralGetTextMetrics() Pin
SLiDeR8-Aug-03 2:48
SLiDeR8-Aug-03 2:48 
GeneralRe: GetTextMetrics() Pin
HPSI8-Aug-03 4:45
HPSI8-Aug-03 4:45 
GeneralGetting if CHtmlView succedded opening an URL Pin
gcolsani8-Aug-03 2:34
gcolsani8-Aug-03 2:34 
GeneralRe: Getting if CHtmlView succedded opening an URL Pin
Neville Franks8-Aug-03 11:24
Neville Franks8-Aug-03 11:24 
GeneralDetecting network interface changes. Pin
jugurd8-Aug-03 2:29
jugurd8-Aug-03 2:29 
GeneralCall function that copy strings Pin
lano18-Aug-03 2:18
lano18-Aug-03 2:18 
GeneralRe: Call function that copy strings Pin
gcolsani8-Aug-03 2:38
gcolsani8-Aug-03 2:38 
GeneralRe: Call function that copy strings Pin
keegan8-Aug-03 4:20
keegan8-Aug-03 4:20 
GeneralRe: Call function that copy strings Pin
Anthony_Yio11-Aug-03 1:44
Anthony_Yio11-Aug-03 1:44 
GeneralControlling an external window... Pin
Kayembi8-Aug-03 1:58
Kayembi8-Aug-03 1:58 
GeneralRe: Controlling an external window... Pin
Kayembi8-Aug-03 5:49
Kayembi8-Aug-03 5:49 
GeneralOLE Automation of MS Access problem. Pin
vcseeker8-Aug-03 1:37
vcseeker8-Aug-03 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.