Examples - C++ threads

Example of creating a thread: This code is written on the basis of the Microsoft Foundation Class Library (MFC).

void CThreadDlg::OnBnClickedButton1()
{
	// TODO: Add control notification handler code here
	if(h_thread == NULL)
	{
		h_thread = CreateThread(
			NULL, 0, &ThreadProc, this, 0, NULL); 		
	}
}
suspend thread
SuspendThread(h_thread);
resume thread
ResumeThread(h_thread);
end thread
TerminateThread(h_thread, 0);
CloseHandle(h_thread);
thread function
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
	CThreadDlg * pthis = (CThreadDlg *) lpParameter;
	while(1)
	{
		pthis->m_go.StepIt();
		Sleep(100);
	}
	return 0;
}

m_go is the control variable.
What you write in the thread function is what you want the thread to do.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853725&siteId=291194637