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 8135191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:20:19+00:00 2026-06-06T10:20:19+00:00

Is there a thread limit EnterCriticalSection() can cope with? The following code works fine

  • 0

Is there a thread limit EnterCriticalSection() can cope with?
The following code works fine with 64 threads, but it crashes when using 65 or more:

CRITICAL_SECTION TestCritSection;

unsigned int threadId;

int getThreadId()
{
    int tid = -1;

    EnterCriticalSection(&TestCritSection);

    tid= threadId;
    threadId++;

    LeaveCriticalSection(&TestCritSection);

    return tid;
}

void parallelTest()
{
    int tid = getThreadId();
    cout << "Thread " << tid << " executed" << endl;
}


void
multiThreadTest()
{
    unsigned int numThreads = 64; // fine, but program crashes when numThreads is set to 65 or more
    HANDLE *threads = new HANDLE[numThreads];
    DWORD ThreadID;
    threadId = 1;

    if (!InitializeCriticalSectionAndSpinCount(&TestCritSection, 0x00000400)) return;

    for (int i=0; i<numThreads; ++i)
    {
        threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) parallelTest, (LPVOID) NULL, 0, &ThreadID);
    }

    WaitForMultipleObjects(numThreads, threads, TRUE, INFINITE);

    DeleteCriticalSection(&TestCritSection);

    for (int i=0; i<numThreads; ++i)
    {
        CloseHandle(threads[i]);
    }

    delete [] threads;
}

I guess CRITICAL_SECTION is internally using a semaphore with a max count of 64.
Can I somehow change that?

  • 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-06T10:20:21+00:00Added an answer on June 6, 2026 at 10:20 am

    As nobody wants to answer my question, I will do it myself based on HansPassant’s comment to my question.

    He pointed out that the problem is WaitForMultipleObjects(), which takes a parameter

    nCount [in]

    The number of object handles in the array pointed to by lpHandles.
    

    The maximum number of object handles is MAXIMUM_WAIT_OBJECTS. This
    parameter cannot be zero.

    (from msdn)

    MAXIMUM_WAIT_OBJECTS is defined as 64, more informations in this thread.

    This means: yes, there is a hard-coded limit on the amount of threads, but the restriction is not on EnterCriticalSection but on WaitForMultipleObjects, which returns an error code I should have checked.

    Here is more information on how to get more than 64 threads working in parallel.

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

Sidebar

Related Questions

Is there an upper limit to how long you can specify a thread to
[Note: There is another thread about this problem but it did not answer the
Is there a way to kill a child thread after some specified time limit
Is there a limit to the number of critical sections I can initialize and
My java code use several threads, each thread run a ServerSocket and goes in
Is there a limit to the rows that IEnumerable.Count() (or IQueryable.Count()) using LINQ to
Is there a thread-safe, non-blocking queue class in the C++? Probably a basic question
Is there a Thread-Local Storage (TLS) equivalent for kernel-mode drivers in Windows (Win32 to
UPDATED: Is there a thread-safe, lock-free and available on all Linux distros increment function
i want to show an alertview while other thread(in other thread there is text

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.