⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 numclass.cpp

📁 Windows多线程编码随书源码
💻 CPP
字号:
/*
 * NumClass.cpp
 *
 * Sample code for "Multithreading Applications in Win32"
 * This is from Chapter 10, Listing 10-3
 *
 * Demonstrate worker thread startup in MFC
 * without AfxBeginThread.
 *
 * Compile with the IDE or: nmake -f NumClass.mak
 */

#include <afxwin.h>

CWinApp TheApp;


class CUserThread : public CWinThread
{
public:  // Member functions
    CUserThread(AFX_THREADPROC pfnThreadProc);

    static UINT ThreadFunc(LPVOID param);

public:  // Member data
    int m_nStartCounter;

private: // The "real" startup function
    virtual void Go();
};

CUserThread::CUserThread(AFX_THREADPROC pfnThreadProc)
 : CWinThread(pfnThreadProc, NULL)  // Undocumented constructor
{
    m_bAutoDelete = FALSE;

    // Set the pointer to the class to be the startup value.
    // m_pThreadParams is undocumented,
    // but there is no work-around.
    m_pThreadParams = this;
}


int main()
{
    CUserThread* pThreads[5];

    for (int i=0; i<5; i++)
    {
        // Pass our static member as the startup function
        pThreads[i] = new CUserThread( CUserThread::ThreadFunc );

        // Set the appropriate member variable
        pThreads[i]->m_nStartCounter = i;

        // Start the thread in motion
        VERIFY(
          pThreads[i]->CreateThread() );
        printf("Thread launched %d\n", i);
    }

    for (i=0; i<5; i++)
    {
        WaitForSingleObject(pThreads[i]->m_hThread, INFINITE);
        delete pThreads[i];
    }

    return 0;
}


// static
UINT CUserThread::ThreadFunc(LPVOID n)
{
    CUserThread* pThread = (CUserThread*)n;
    pThread->Go();
    return 0;
}

void CUserThread::Go()
{
    int n = m_nStartCounter;
    for (int i=0;i<10;i++)
        printf("%d%d%d%d%d%d%d%d\n",n,n,n,n,n,n,n,n);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -