win32 API for Multithreading
1.API function
(1) CreateThread creates threads
(2) SuspendThread suspended thread
(3) ResumeThread terminates the suspended state of the thread and executes the thread
(4) ExitThread is used to terminate the execution of the thread itself, mainly called in the execution function of the thread.
(5) TerminateThread forcibly terminates the execution of a thread. Note: However, it is not recommended to use this function to terminate the execution of threads immediately, but it does not release the resources occupied by threads.
(6) PostThreadMessage, which places a message in the message queue of the specified thread and does not wait until the message is processed by the thread. If the thread that is about to receive the message does not create a message loop, the function fails to execute.
Specific functions you can Baidu, it should be easy to find the exact definition of each function.
2. Programming examples
2.1 example 1
Create a dialog box, add two buttons and an edit box on the dialog box with IDC_START, IDC_STOP and IDC_TIME, respectively.
Head file:
Add code outside the class:
void ThreadFunc(); //Thread function static BOOL m_bRun; //global variable
Dialog box class code:
HANDLE hThread; //Handle to thread function DWORD ThreadID; //ID of thread function
Source files:
Add code outside the class:
//Thread function void ThreadFunc() { CTime time; CString strTime; m_bRun = TRUE; while (m_bRun) { time = CTime::GetCurrentTime(); strTime = time.Format("%Y-%m-%d %H:%M:%S %W-%A"); ::SetDlgItemText(AfxGetApp()->m_pMainWnd->m_hWnd, IDC_TIME, strTime);//AfxGetApp () - > m_pMainWnd - > m_hWnd queries the main window handle of the current thread Sleep(1000); } }
Add code to the class:
//Starting function void CMultiThread1Dlg::OnBnClickedStart() { // TODO: Add control notification handler code here hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,NULL,0,&ThreadID); //Create thread functions GetDlgItem(IDC_START)->EnableWindow(FALSE); GetDlgItem(IDC_STOP)->EnableWindow(TRUE); } //Pause function void CMultiThread1Dlg::OnBnClickedStop() { // TODO: Add control notification handler code here m_bRun = FALSE; GetDlgItem(IDC_START)->EnableWindow(TRUE); GetDlgItem(IDC_STOP)->EnableWindow(FALSE); }
1.2.2 Example 2
Create a dialog box, add a button and an edit box on the dialog box with IDC_START and IDC_COUNT, respectively. And add int variable, m_nCount, to the edit box.
Head file:
Add code outside the class:
void ThreadFunc(int integer); //Thread function
Dialog box class code:
HANDLE hThread; //Handles to Thread Functions DWORD ThreadID; //ID of thread function
Source files:
Add code outside the class:
//Thread function void ThreadFunc(int integer) { int i; for ( i = 0; i < integer; i++) { Sleep(1000); } }
Add code to the class:
//Message function void CMultiThread2Dlg::OnBnClickedStart() { // TODO: Add control notification handler code here UpdateData(TRUE); int interger = m_nCount; hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,(VOID*)interger,0,&ThreadID); GetDlgItem(IDC_START)->EnableWindow(FALSE); WaitForSingleObject(hThread,INFINITE); GetDlgItem(IDC_START)->EnableWindow(TRUE); }
1.2.3 Example 3
Create a dialog box and add a button, an edit box and a progress bar to the dialog box with IDC_MILLISECOND, IDC_PROGRESS1 and IDC_START, respectively. And add int variables to the edit box, m_nMilliSecond; add CProgress Ctrl variables to the progress, m_ctrl Progress.
Head file:
Add variables outside the class:
struct threadinfo { UINT nMilliSecond; CProgressCtrl *pctrlProgess; }; //Thread function UINT ThreadFunc(LPVOID lpParam); static threadinfo Info; //Variables to be used by threads
Add variables within the class:
HANDLE hThread; //thread handle DWORD ThreadID; //Thread ID int m_nMilliSecond; CProgressCtrl m_ctrlProgress;
Source files:
Add code outside the class:
//Thread function UINT ThreadFunc(LPVOID lpParam) { threadinfo *pInfo = (threadinfo *)lpParam; for (int i = 0; i < 100; i++) { int nTemp = pInfo->nMilliSecond; pInfo->pctrlProgess->SetPos(i); Sleep(nTemp*10); } return 0; }
Add code to the class:
//Message function void CMultiThread3Dlg::OnBnClickedStart() { // TODO: Add control notification handler code here UpdateData(FALSE); Info.nMilliSecond = m_nMilliSecond; Info.pctrlProgess = &m_ctrlProgress; hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,&Info,0,&ThreadID); }
To be precise, these codes are not original, but refer to a document "Introduction to Multithread Programming". But according to their own requirements have also changed, also pretend to be original.