Click here to Skip to main content
15,916,449 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: ActiveX in a view Pin
Yeskay3-Jan-08 21:42
Yeskay3-Jan-08 21:42 
GeneralRe: ActiveX in a view Pin
Maxwell Chen3-Jan-08 21:49
Maxwell Chen3-Jan-08 21:49 
QuestionRe: ActiveX in a view Pin
Yeskay6-Jan-08 16:41
Yeskay6-Jan-08 16:41 
GeneralRunning constant number of threads. Pin
Evgeni573-Jan-08 21:10
Evgeni573-Jan-08 21:10 
GeneralRe: Running constant number of threads. Pin
Maxwell Chen3-Jan-08 21:23
Maxwell Chen3-Jan-08 21:23 
GeneralRe: Running constant number of threads. Pin
Evgeni573-Jan-08 21:30
Evgeni573-Jan-08 21:30 
GeneralRe: Running constant number of threads. Pin
Maxwell Chen3-Jan-08 21:32
Maxwell Chen3-Jan-08 21:32 
GeneralRe: Running constant number of threads. Pin
Member 7549604-Jan-08 6:13
Member 7549604-Jan-08 6:13 
The problems in the code are various. There are problems with basic thread and semaphore managment. I have to guess at what the code is trying to do: create up to 20 simultaneous threads and wait until until a maximum of 100 threads have been created. Each thread uses a semaphore to block thread termination.

There are problems with initialization, creating and destroying threads and semaphores, and using the semaphores for thread communication.

Initialization

Both the handles for the thread and the semaphore aren't initialized.

The logic of Initilize (check spelling) looks flawed. If the call to CreateSemaphore is sucessful (which should be checked) then the handle returned is non-null. In that case Release is called which then closes the handle just created but does not reset the variable. So it looks like the program starts off with a bogus handle to an invalid semaphore.

Release() will try to close the threadHandle, which was never initialized, and loses access to the thread without knowing if it ever started or terminated properly. So at the end of a call to Release, assuming that CloseHandle does not raise an exception, threadHandle is invalid. Release is called after every unsuccessful call to CreateThread and threadHandle is invalid (NULL). So Release should be closing an invalid handle which should raise an exception.

Semaphore management

Assuming the initialization issues are dealt with, we can look at how the semaphore is managed. The semaphore is a "count up" value that signals when non zero. The semaphore is created in the non signaled state. Each thread begins by waiting until the semaphore is signaled and only then calls ReleaseSemaphore which increments the semaphore count and signals any other waiting threads. So the first thread will block on the sempahore. So will the second amd every other thread will because the sempahore was created in the non-signaled state. All threads wait for other threads to end and nobody ends.

Thread management

The StartSimulation is pretty flawed. The infinite loop and WaitForSingleObject with INFINITE time are problems waiting to happen. The exit condition for the while loop is when the counter variable reaches 100. The counter is incremented every time an attempt to create a thread is made. Attempts to create a thread are controlled by the limit variable. While it is less than maxNumberOfThreads threads are created. As each thread ends, limit is decremented. Since no thread will ever end due to the semaphore block, the variable quickly reaches the maximum of 20 threads. At that point the program goes into the infinite loop.

My recomendation is to use 2 semaphores and test each to be sure you know
how they work. One semaphore to control thread termination and one to control thread creation. Look closely at ReleaseSemaphore and pay attention to the 3rd argument. Check return values and don't wait forever on events.
GeneralRe: Running constant number of threads. Pin
Member 7549604-Jan-08 9:33
Member 7549604-Jan-08 9:33 
GeneralCreate User Pin
narayanagvs3-Jan-08 20:54
narayanagvs3-Jan-08 20:54 
QuestionRe: Create User Pin
David Crow4-Jan-08 3:03
David Crow4-Jan-08 3:03 
GeneralTransparent CheckBox Pin
nitin33-Jan-08 20:41
nitin33-Jan-08 20:41 
Generalchange the color of Toolbar button controls Pin
msr_codeproject3-Jan-08 20:35
msr_codeproject3-Jan-08 20:35 
GeneralRe: change the color of Toolbar button controls Pin
Mark Salsbery4-Jan-08 7:26
Mark Salsbery4-Jan-08 7:26 
QuestionCan MSDN local work like MSDN on msdn2? Pin
fantasy12153-Jan-08 20:18
fantasy12153-Jan-08 20:18 
GeneralRe: Can MSDN local work like MSDN on msdn2? Pin
Maxwell Chen3-Jan-08 20:24
Maxwell Chen3-Jan-08 20:24 
Generalneed your help Pin
gentleguy3-Jan-08 19:49
gentleguy3-Jan-08 19:49 
GeneralRe: need your help Pin
Maxwell Chen3-Jan-08 20:01
Maxwell Chen3-Jan-08 20:01 
GeneralRe: need your help Pin
gentleguy3-Jan-08 23:58
gentleguy3-Jan-08 23:58 
GeneralRe: need your help Pin
Maxwell Chen4-Jan-08 0:18
Maxwell Chen4-Jan-08 0:18 
GeneralRe: need your help Pin
Hamid_RT3-Jan-08 20:11
Hamid_RT3-Jan-08 20:11 
GeneralRe: need your help Pin
gentleguy3-Jan-08 21:19
gentleguy3-Jan-08 21:19 
GeneralRe: need your help Pin
Maxwell Chen3-Jan-08 21:25
Maxwell Chen3-Jan-08 21:25 
QuestionRe: need your help Pin
David Crow4-Jan-08 3:07
David Crow4-Jan-08 3:07 
GeneralRe: need your help Pin
gentleguy4-Jan-08 19:35
gentleguy4-Jan-08 19:35 

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.