Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Critical Sections

Critical Sections

Scheduled Pinned Locked Moved C / C++ / MFC
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Patje
    wrote on last edited by
    #1

    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
    
    N 1 Reply Last reply
    0
    • P Patje

      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
      
      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      My guess is that the profiler isn't telling the truth. The fact that it isn't showing EnterCriticalSection rings alarm bells. Run the program in the debugger and verify that EnterCriticalSection is indeed being called. It must be. If there is a bottleneck it will be at EnterCriticalSection when another thread has it locked. LeaveCriticalSection should be instantaneous. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups