Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:32:51+00:00 2026-06-07T09:32:51+00:00

I wrote a small program to compare the performance of Critical Section vs Mutex

  • 0

I wrote a small program to compare the performance of Critical Section vs Mutex in Windows.

On the tests I ran, acquiring Critical Section seems to be Slower :O Can anybody explain why both things take almost the same amount of time, and what is happening internally.

This is the timer I used – http://cplus.about.com/od/howtodothingsi2/a/timing.htm

#include "stdafx.h"
#include<iostream>
#include<vector>
#include "h_timer.h"
#include<WinBase.h>
#include<Windows.h>
#include<stdio.h>

#define MAX_THREADS 2000  
//Comment and Uncomment this to enable/disable critialSection / Mutex
#define CRIT 1

using namespace std;

HANDLE Mutex;
CRITICAL_SECTION critSection;
DWORD WINAPI Contention( LPVOID );

int main( void )
{
    HANDLE Thread[MAX_THREADS];
    DWORD ThreadID;
    int i;

#ifdef CRIT
//create a critical section
InitializeCriticalSection(&critSection);
#else

    // Create a mutex with no initial owner
    Mutex = CreateMutex( NULL, FALSE,NULL);

#endif

    // Create worker threads

CStopWatch timer, tempTimer;
timer.startTimer();
    for( i=0; i < MAX_THREADS; i++ )
    {
        Thread[i] = CreateThread( NULL,
                     0,(LPTHREAD_START_ROUTINE)Contention,NULL,0,&ThreadID);
    }
    WaitForMultipleObjects(MAX_THREADS, Thread, TRUE, INFINITE);

    timer.stopTimer();
    cout<<endl<<"Elapsed Time:"<<timer.getElapsedTime();
    cin.get();
    // Close thread and mutex handles

    for( i=0; i < MAX_THREADS; i++ )
    CloseHandle(Thread[i]);

    CloseHandle(Mutex);
    return 0;
}


DWORD WINAPI Contention( LPVOID lpParam )
{
    #ifdef CRIT
EnterCriticalSection(&critSection);
//printf("ThreadId: %d\n",GetCurrentThreadId());
//printf("Let's try Again. %d\n\n", GetCurrentThreadId());
LeaveCriticalSection(&critSection);

#else
// lpParam not used in this example
    UNREFERENCED_PARAMETER(lpParam);
    DWORD dwCount=0, dwWaitResult;

    // Request ownership of mutex.
    dwWaitResult = WaitForSingleObject(
            ghMutex, // handle to mutex
            INFINITE); // no time-out interval
        dwCount++;
    ReleaseMutex(ghMutex);
#endif

return TRUE;
}

For 2000 threads, on a Quad Core HPZ210, both take roughly 1.5 secs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:32:52+00:00Added an answer on June 7, 2026 at 9:32 am

    I think there are two factors:

    Mainly – Your program is dominated by thread creation overhead. You are creating and destroying 2000 threads, and only accessing the mutex/CS once per thread. The time spent creating threads swamps the difference in lock/unlock times.

    Also – You may not be testing the use case that these locks were optimized for. Try spawning two threads that each try to access the mutex/CS thousands of times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small program wrote in Prolog. At the moment I can print
I wrote a small C++ program in VS2k8. When I launch it from windows
I wrote a small program, that creates files at an interval of 1 minute.
I wrote a small program to test accessing a widget parent's slot. Basically, it
I wrote a small test program for flipperview. I have 3 views. I call
I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
So I wrote a project-management program for a small business using Microsoft Access 2007.
I wrote a small program using a custom indexOf function but wanted to dismiss
I wrote a small program in VS2005 to test whether C++ global operator new
I wrote a small program that uses Mechanize to traverse a site. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.