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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:36:01+00:00 2026-05-13T22:36:01+00:00

(In short: main()’s WaitForSingleObject hangs in the program below). I’m trying to write a

  • 0

(In short: main()’s WaitForSingleObject hangs in the program below).

I’m trying to write a piece of code that dispatches threads and waits for them to finish before it resumes. Instead of creating the threads every time, which is costly, I put them to sleep. The main thread creates X threads in CREATE_SUSPENDED state.

The synch is done with a semaphore with X as MaximumCount. The semaphore’s counter is put down to zero and the threads are dispatched. The threds perform some silly loop and call ReleaseSemaphore before they go to sleep. Then the main thread uses WaitForSingleObject X times to be sure every thread finished its job and is sleeping. Then it loops and does it all again.

From time to time the program does not exit. When I beak the program I can see that WaitForSingleObject hangs. This means that a thread’s ReleaseSemaphore did not work. Nothing is printf’ed so supposedly nothing went wrong.

Maybe two threads shouldn’t call ReleaseSemaphore at the exact same time, but that would nullify the purpose of semaphores…

I just don’t grok it…

Other solutions to synch threads are gratefully accepted!

#define TRY  100
#define LOOP 100

HANDLE *ids;
HANDLE semaphore;

DWORD WINAPI Count(__in LPVOID lpParameter)
{ 
 float x = 1.0f;   
 while(1)
 { 
  for (int i=1 ; i<LOOP ; i++)
   x = sqrt((float)i*x);
  while (ReleaseSemaphore(semaphore,1,NULL) == FALSE)
   printf(" ReleaseSemaphore error : %d ", GetLastError());
  SuspendThread(ids[(int) lpParameter]);
 }
 return (DWORD)(int)x;
}

int main()
{
 SYSTEM_INFO sysinfo;
 GetSystemInfo( &sysinfo );
 int numCPU = sysinfo.dwNumberOfProcessors;

 semaphore = CreateSemaphore(NULL, numCPU, numCPU, NULL);
 ids = new HANDLE[numCPU];

 for (int j=0 ; j<numCPU ; j++)
  ids[j] = CreateThread(NULL, 0, Count, (LPVOID)j, CREATE_SUSPENDED, NULL);

 for (int j=0 ; j<TRY ; j++)
 {
  for (int i=0 ; i<numCPU ; i++)
  {
   if (WaitForSingleObject(semaphore,1) == WAIT_TIMEOUT)
    printf("Timed out !!!\n");
   ResumeThread(ids[i]);  
  }
  for (int i=0 ; i<numCPU ; i++)
   WaitForSingleObject(semaphore,INFINITE);
  ReleaseSemaphore(semaphore,numCPU,NULL);
 }
 CloseHandle(semaphore);
 printf("Done\n");
 getc(stdin);
}
  • 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-05-13T22:36:01+00:00Added an answer on May 13, 2026 at 10:36 pm

    the problem happens in the following case:

    the main thread resumes the worker threads:

      for (int i=0 ; i<numCPU ; i++)
      {
       if (WaitForSingleObject(semaphore,1) == WAIT_TIMEOUT)
        printf("Timed out !!!\n");
       ResumeThread(ids[i]);  
      }
    

    the worker threads do their work and release the semaphore:

      for (int i=1 ; i<LOOP ; i++)
       x = sqrt((float)i*x);
      while (ReleaseSemaphore(semaphore,1,NULL) == FALSE)
    

    the main thread waits for all worker threads and resets the semaphore:

      for (int i=0 ; i<numCPU ; i++)
       WaitForSingleObject(semaphore,INFINITE);
      ReleaseSemaphore(semaphore,numCPU,NULL);
    

    the main thread goes into the next round, trying to resume the worker threads (note that the worker threads haven’t event suspended themselves yet! this is where the problem starts… you are trying to resume threads that aren’t necessarily suspended yet):

      for (int i=0 ; i<numCPU ; i++)
      {
       if (WaitForSingleObject(semaphore,1) == WAIT_TIMEOUT)
        printf("Timed out !!!\n");
       ResumeThread(ids[i]);  
      }
    

    finally the worker threads suspend themselves (although they should already start the next round):

      SuspendThread(ids[(int) lpParameter]);
    

    and the main thread waits forever since all workers are suspended now:

      for (int i=0 ; i<numCPU ; i++)
       WaitForSingleObject(semaphore,INFINITE);
    

    here’s a link that shows how to correctly solve producer/consumer problems:

    http://en.wikipedia.org/wiki/Producer-consumer_problem

    also i think critical sections are much faster than semaphores and mutexes. they’re also easier to understand in most cases (imo).

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

Sidebar

Related Questions

In following code, #include<stdio.h> int main() { short a[2]={5,10}; short *p=&a[1]; short *dp=&p; printf(%p\n,p);
I tried many solutions, but can't get my program work. main.h short NWMP_acc[3]; short
I have the following code snippet. public static void main(String[] args) { short a
I wrote this short program int main(){ char * c = abcd; c[1] =
The short program below is intended to iterate over argv passed from the command
Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char*
In short, I would like to, in Objective-C cocoa, program something that functions the
The following code example demonstrates that the implicit cast from short to char fires
What I want in short is: Core Data that runs without blocking the main
Consider this code: public class ShortDivision { public static void main(String[] args) { short

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.