Saturday, June 25, 2011

Old School VS .net 2005 MFC Threading and Timers

I come from a background of multithreading with Java. I also did some threading with pthreads.

CWinThreads don't seem to have the same level of control that java has.

To create a CWinThread, use this :

AfxBeginThread(ProcName, NULL, THREAD_PRIORITY_NORMAL, 0, 0, NULL);

Using AfxBeginThread allows you to access MFC objects.

Make sure you create global function:


UINT ProcName(LPVOID param)

{

for(int i=0; i<100000000; i++)

{
if (i % 10000000 == 0){

TRACE("thread 2 id = %d, %d \n",AfxGetThread()->m_nThreadID,i);
}

}

return 0;
}


You probably noticed the AfxGetThread() function which gives you access to the current thread. Very helpful function.

It looks like CWinThread uses preemptive multitasking but it's kind of slow, doing like context switches every second or so.

I also played with the MFC timer. This doesn't create a new thread whenever the timer function executes. It uses the same thread as is used in the GUI (I was using a dialog project). This is also much too slow. You can set the timer to every millisec. Would be nice to have it down to microsec.

No comments:

Post a Comment