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

📄 ftptest.cpp

📁 window下的多线程编程参考书。值得一读
💻 CPP
字号:
//
// FILE: FTPTest.cpp
//
// Copyright (c) 1997 by Aaron Michael Cohen and Mike Woodring
//
/////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include "CFixedThreadPool.h"

// Thread handler that will be used for all threads dispatched
// from the thread pool.
//
class CFixedPoolThreadHandler : public CMclThreadHandler
{
    public:
        virtual unsigned ThreadHandlerProc()
        {
            // Do some "work" by sleeping between 1 and 5 seconds.
            //
            srand(GetCurrentThreadId());

            DWORD dwWorkPeriod = (1000 + (rand() % 4000));

            printf(
                "[0x%08lx] Thread has been dispatched.  Sleeping %dms\n",
                GetCurrentThreadId(),
                dwWorkPeriod
            );

            Sleep(dwWorkPeriod);

            printf(
                "[0x%08lx] Thread is done executing.  Returning to the pool.\n",
                GetCurrentThreadId()
            );

            return(0);
        }
};

void main( void )
{
    CFixedThreadPool        ThreadPool(4);
    CFixedPoolThreadHandler ThreadHandler;

    printf(
        "[0x%08lx] Primary thread is dispatching jobs...\n",
        GetCurrentThreadId()
    );

    // Dispatch all of the threads in the pool to do a job.
    //
    while( ThreadPool.DispatchThread(&ThreadHandler) )
    {
        ;
    }

    // Sleep for an arbitrary amount of time.  If we exit the primary
    // thread while threads in the pool are still running, the destructor
    // for CFixedThreadPool will block our thread until every thread
    // in the pool has returned.
    //
    srand(GetCurrentThreadId());
    DWORD dwPrimaryThreadLifetime = 5 + (rand() % 3);

    printf(
        "[0x%08lx] Primary thread going to sleep for %d seconds.\n",
        GetCurrentThreadId(),
        dwPrimaryThreadLifetime * 1000
    );

    Sleep(dwPrimaryThreadLifetime * 1000);
    
    printf(
        "[0x%08lx] Primary thread about to destroy thread pool.\n",
        GetCurrentThreadId()
    );
}

⌨️ 快捷键说明

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