Here is a copy-paste template to test your code for multithreading. Let's say you want to test your function
int f() { ... }for multithreading. Use this code to test f() running from number of threads simultaneously:
struct TESTPARAM
{
int threadNo;
LONG* m_pnCount;
};
DWORD WINAPI _msgTestThread(void* pParam)
{
TESTPARAM* par = (TESTPARAM*)pParam;
char buf[1014];
int tmax = 1000;
int tstep = 100;
for (int t = 0; t<tmax; t+=tstep)
{
f();
Sleep(tstep);
}
InterlockedDecrement(par->m_pnCount);
delete par;
return 0;
};
int Start()
{
LONG nThreadCount = 0;
int THREAD_MAX = 10;
//msg::Message threads
for (int i = 0; i<THREAD_MAX; i++)
{
//new, otherwise TESTPARAM will not reach thread in _msgTestThread,
//and will be deleted on closing scope }
TESTPARAM * par = new TESTPARAM;
par->threadNo = i;
par->m_pnCount = &nThreadCount;
InterlockedIncrement(par->m_pnCount);
DWORD dwThreadID = 0;
CreateThread(0, 0, _msgTestThread, par, 0, &dwThreadID);
}
//Call window procedure until threads are finished
while (nThreadCount > 0)
{
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Sleep(10);
}
}
No comments:
Post a Comment