Click here to Skip to main content
15,914,820 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
GeneralRe: Running constant number of threads. Pin
Member 7549604-Jan-08 9:33
Member 7549604-Jan-08 9:33 
Given the above, we change to code to get the program to run.
N.B. make sure you compile the code as a multi-threaded app.

const long MAXTHREADS = 100L;
const long MAXACTIVETHREADS = 20L;

int nThread = 0;
int nReleased = 0;
long lActiveThreads = 0L;
long lPreviousCount = 0L;

HANDLE hSemaphore = 0;
HANDLE hThread = 0;

const char *pSemaphoreName = "CreateThreadSemaphore";

bool Initialize()
{
	bool bResult = false;
	hSemaphore = ::CreateSemaphore(NULL, 0, MAXACTIVETHREADS, pSemaphoreName);
	bResult = (0 != hSemaphore);
	if (!bResult) {
		// TODO: report error from GetLastError()
	}

	return bResult;
}

DWORD WINAPI Start( LPVOID lpParam )
{
	// block til semaphore signals
	::WaitForSingleObject(hSemaphore, INFINITE);
	printf("End thread %d\n", GetCurrentThreadId());
	--lActiveThreads;

	++nReleased;
	::ReleaseSemaphore(hSemaphore, 1, &lPreviousCount);

	return TRUE;
}

void StartSimulation()
{
	while (nThread < MAXTHREADS) {
		if (lActiveThreads < MAXACTIVETHREADS) {
			DWORD dwThreadID = 0;
			hThread = ::CreateThread(NULL, 0, Start, NULL, 0, &dwThreadID); 
			if (!hThread) {
				// TODO: report error from GetLastError()
			} else {
				printf("Start thread %d\n", dwThreadID);
				::CloseHandle(hThread);	// throw away thread handle
				++lActiveThreads;
				++nThread;
			}
		} else {
			::ReleaseSemaphore(hSemaphore, 1, &lPreviousCount);
		}
	}

	// wait for all threads to end
	while (lActiveThreads) {
		::ReleaseSemaphore(hSemaphore, 1, &lPreviousCount);
	}

	// destroy hSemaphore
	::CloseHandle(hSemaphore);
	
	printf("Created %d threads.\n"
		"Release %d threads.\n"
		"PreviousCount %ld\n", nThread, nReleased, lPreviousCount);

}

int main()
{
	if (Initialize())
		StartSimulation();

	return 0;
}


I changed variable names and removed unnecessary code but didn't implement all the changes I indicated. The output should give you some idea of how running threads behave.
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 
QuestionHow to show in messagebox same chinese text from rtf . Pin
johnalek3-Jan-08 19:43
johnalek3-Jan-08 19:43 

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.