Multi-threading in C++:

mwin

2[H]4U
Joined
Jun 24, 2004
Messages
3,377
I'm trying to figure out why this won't work. I've coded up a little controller for my Matrix Orbital LCD display. Part of the deal is a CPU usage monitor. I was trying to spawn a new thread for the monitor's process so the program would still be responsive. Here's the code in question:

This is where I declare and start the monitor:
Code:
void CLCDControllerDlg::OnBnClickedButton1()
{
	CLCDControls theControls;

	theControls.StartCpuMonitor();
	MessageBox("After StartCpuMonitor in OnBnClickedButton1()", "Debug Msg", IDOK);
}

This is the StartCpuMonitor function in my CLCDControls class:
Code:
void CLCDControls::StartCpuMonitor()
{
	LPDWORD threadID = NULL;
	
	SHCreateThread((LPTHREAD_START_ROUTINE)MonitorThreadFunc(), NULL, NULL, NULL);

	MessageBox(NULL, "After CreateThread in StartCpuMonitor()", "Debug Msg", IDOK);
}

This is the MonitorThreadFunc() function, also in the CLCDControls class:
Code:
unsigned long CLCDControls::MonitorThreadFunc(void)
{
	unsigned long byteCount = 0;
	CString useMsg;
	int usage = 0, lastUsage = 0;
	double diff = 0;
	CCpuUsage theMonitor;

	byteCount += ClearDisplay();
	byteCount += InitGraph();
	byteCount += WriteLine((BYTE*)"CPU Use: ");

	theMonitor.InitMonitor();
	while (ContinueCpuMonitor)
	{
		usage = theMonitor.GetUsage();
		diff = usage - lastUsage;
		
		//Write the percentage out to the screen
		useMsg.Format("%3d", usage);
		useMsg += '%';
		MoveCursor(10, 1);
		byteCount += Write((BYTE*)useMsg.GetBuffer(), useMsg.GetLength());

		//Smoothely move from lastUsage to usage over the next
		//   half second.
		diff = diff / 5;
		for (double i = 1; i <= 5; i++)
		{
			int percent = lastUsage + (diff * i);
			byteCount += DrawGraph(1, 2, 0, percent);
			Sleep(100);
		}

		lastUsage = usage;
	}

	return(byteCount);
}

Neither of the message boxes ever pop up. The thread monitoring function does begin execution, but it doesn't appear to be a new thread. Or it's somehow completely stealing control from the calling function(s). Anybody see what I'm missing? This is using Visual Studio 2003, MFC, C++. Thanks in advance.
 
Have you examined the documentation (http://msdn2.microsoft.com/en-us/library/ms538419.aspx) for the SHCreateThread function?

I am not a Windows API coder but it appears the function you are passing as the first argument does not adhere to the correct format:

The function pointed to by pfnThreadProc and pfnCallback must take the following form.
Code:
    DWORD WINAPI ThreadProc(LPVOID pData)
    {
      ...
    }
The function name is arbitrary. The pData parameter points to an application-defined data structure with initialization information.
 
If you're using MFC, why are you using SHCreateThread()? I'd expect to see you using AfxBeginThread() and CWinThread.

I wrote AfxBeginThread() and most of CWinThread(), so if you have questions about it, let me know. It's all covered in my book, too.
 
I'm really new to this whole multithreading thing. Pretty much just trying to pick it up on my own. We did something real simple in my Operating Systems class, but that was years ago and I couldn't find that project. I'll look up the documentation for the Afx functions. Thanks for the direction.
 
I got the multithreading part working. So thanks for that.

Unfortunately, this happened. I'll have to put the project on hold for a while. Which sucks.
 
Great. Let us know if you have more questions.
 
Back
Top