哪个线程用于多线程的相同功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了哪个线程用于多线程的相同功能相关的知识,希望对你有一定的参考价值。

在mfc代码中,

声明如下

CWinThread* _pThread[5];
DWORD _ThreadArg[5] = { HIGHEST_THREAD, // 0x00
                    ABOVE_AVE_THREAD, // 0x3F
                    NORMAL_THREAD, // 0x7F
                    BELOW_AVE_THREAD, // 0xBF
                    LOWEST_THREAD // 0xFF
                    }; 

int i;
for (i= 0; i< 5; i++)
{
    _pThread[i] = AfxBeginThread(ThreadFunc,
                                &_ThreadArg[i],
                                THREAD_PRIORITY_NORMAL,
                                0,
                                //CREATE_SUSPENDED,
                                NULL,
                                NULL);
    //_pThread[i]->ResumeThread();
}
// setup relative priority of threads
_pThread[0]->SetThreadPriority(THREAD_PRIORITY_HIGHEST);
_pThread[1]->SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL);
_pThread[2]->SetThreadPriority(THREAD_PRIORITY_NORMAL);
_pThread[3]->SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL);
_pThread[4]->SetThreadPriority(THREAD_PRIORITY_LOWEST);

使用相同的线程功能

UINT CThreadPoolDlg::ThreadFuncTh1(LPVOID ThreadArg)
{
    DWORD dwArg = *(DWORD*)ThreadArg;
    //txtTh[0].SetWindowTextW(_T("23"));
    AfxGetMainWnd()->GetDlgItem(IDC_THD1)->SetWindowText(_T("1"));
    return 0;
}

如何在同一功能中检查正在使用的线程?

答案

我得到一个答案如下

_pThread[0] = AfxBeginThread(ThreadFunc,this,
                            THREAD_PRIORITY_NORMAL,
                            0,
                            NULL,
                            NULL);

_pThread[1] = AfxBeginThread(ThreadFunc,this,
                            THREAD_PRIORITY_NORMAL,
                            0,
                            CREATE_SUSPENDED,
                            NULL);

_pThread[2] = AfxBeginThread(ThreadFunc,this,
                            THREAD_PRIORITY_NORMAL,
                            0,
                            CREATE_SUSPENDED,
                            NULL);

线程函数声明如下

UINT CThreadPoolDlg::ThreadFunc(LPVOID ThreadArg)
{
    CThreadPoolDlg* dwArg = (CThreadPoolDlg*)ThreadArg;
    DWORD dThread = GetCurrentThreadId();

    //txtTh[0].SetWindowTextW(_T("23"));
    while(1)
    {   
        CString strTemp;

        if(dThread == dwArg->_pThread[0]->m_nThreadID)
        {           
            AfxGetMainWnd()->GetDlgItem(IDC_THD1)->GetWindowText(strTemp);      
            int n = _wtoi(strTemp);
            strTemp.Format(_T("%d"), ++n);      
            AfxGetMainWnd()->GetDlgItem(IDC_THD1)->SetWindowText(strTemp);

            if(n > 5)
            {
                ::SendMessage(dwArg->GetSafeHwnd(), OWM_MYMETHOD, 0, 0);
                //dwArg->_pThread[1]->ResumeThread();
            }
        }

        if(dThread == dwArg->_pThread[1]->m_nThreadID)
        {           
            AfxGetMainWnd()->GetDlgItem(IDC_THD2)->GetWindowText(strTemp);      
            int n = _wtoi(strTemp);
            strTemp.Format(_T("%d"), ++n);      
            AfxGetMainWnd()->GetDlgItem(IDC_THD2)->SetWindowText(strTemp);

            if(n > 5)
            {
                dwArg->_pThread[2]->ResumeThread();
            }
        }

        if(dThread == dwArg->_pThread[2]->m_nThreadID)
        {
            AfxGetMainWnd()->GetDlgItem(IDC_THD3)->GetWindowText(strTemp);
            int n = _wtoi(strTemp);
            strTemp.Format(_T("%d"), ++n);
            AfxGetMainWnd()->GetDlgItem(IDC_THD3)->SetWindowText(strTemp);
        }

        Sleep(500);
    }
    return 0;
}

看看吧

DWORD dThread = GetCurrentThreadId();
dThread == dwArg->_pThread[1]->m_nThreadID

然后我将获得当前的线程ID ...

以上是关于哪个线程用于多线程的相同功能的主要内容,如果未能解决你的问题,请参考以下文章

多线程的同一个功能使用哪个线程

线程学习知识点总结

多线程 Thread 线程同步 synchronized

多线程基础篇

java多线程(线程通信-等待换新机制-代码优化)

多个请求是多线程吗