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

📄 cfixedthreadpool.h

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

#ifndef __CFixedThreadPool_H__
#define __CFixedThreadPool_H__

#include <CMclGlobal.h>
#include <CMclLinkedLists.h>
#include <CMclThread.h>
#include <CMclCritSec.h>
#include <CMclSemaphore.h>
#include <CMclEvent.h>
#include <CMclWaitableCollection.h>
#include <CMclAutoPtr.h>
#include "CThreadPool.h"

// CFixedThreadPool
//
// This implementation of the CThreadPool abstract base class provides
// for a fixed number of threads that are created once when the pool
// is initialized, and that remain alive until the pool is destroyed.
//
class CFixedThreadPool : public CThreadPool, private CMclThreadHandler
{
   public:
        CFixedThreadPool( long lMaxThreads );
        virtual ~CFixedThreadPool();

        // CThreadPool implementation.
        //
        virtual BOOL DispatchThread( CMclThreadHandler *pHandler );

    private:
        // CMclThreadHandler implementation.
        //
        virtual unsigned ThreadHandlerProc( void );

    private:
        // CDispatchQueue
        //
        // We'll roll our own implementation of a queue that provides
        // for upfront allocation of all resources required to
        // implement the pool.  You can substitute a CMclQueue if
        // you don't need such a stringent implementation.
        //
        class CDispatchQueue
        {
            public:
                struct DISPATCHRECORD
                {
                    DISPATCHRECORD()
                    {
                        pUserThreadHandler = 0;
                    }

                    DISPATCHRECORD( CMclThreadHandler *pThreadHandler )
                    {
                        pUserThreadHandler = pThreadHandler;
                    }

                    CMclThreadHandler  *pUserThreadHandler;
                };

            public:
                CDispatchQueue( long lMaxDepth );
                ~CDispatchQueue();

                BOOL Put( const DISPATCHRECORD& Node );
                BOOL Get( DISPATCHRECORD& Node, CMclEvent *pInterrupt );

            private:
                CMclCritSec         m_CritSec;
                DISPATCHRECORD     *m_pQueue;
                CMclSemaphore       m_SlotFull;
                CMclSemaphore       m_SlotFree;
                long                m_lHeadIndex;
                long                m_lTailIndex;
                long                m_lNumSlots;
        };

    private:
        // Dispatch queue (finite depth).  DispatchThread can fail
        // if the dispatch queue is full.  This allows the dispatch
        // queue to be finite, but larger than the number of threads
        // that are created and managed by the thread pool.
        //
        CDispatchQueue          m_DispatchQueue;

        // Thread management.
        //
        long                    m_lMaxThreads;
        CMclEvent               m_ExitEvent;
        CMclThreadAutoPtr       m_Threads[MAXIMUM_WAIT_OBJECTS];
};

#endif // __CFixedThreadPool_H__

⌨️ 快捷键说明

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