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

📄 cmailboxdemo.cpp

📁 window下的多线程编程参考书。值得一读
💻 CPP
字号:
#include <stdio.h>
#include <tchar.h>
#include "CMcl.h"

#define MAILBOX_NAME        __TEXT("CMailboxDemoMailbox")
#define MAILBOX_DEPTH       3
#define MAILBOX_MSG_SIZE    sizeof(DWORD)
#define MAILBOX_TIMEOUT     5

class CMclMailboxReader : public CMclThreadHandler {
private:
    CMclMailbox m_cMailbox;
    BOOLEAN m_bRun;

public:
    // class constructor initializes the mailbox object...
    CMclMailboxReader( LPCTSTR lpszName) :
        m_cMailbox( MAILBOX_DEPTH, MAILBOX_MSG_SIZE, lpszName) {
            m_bRun = TRUE;
            return;
    }

    virtual unsigned ThreadHandlerProc(void) {
        while (m_bRun) {
            DWORD dwMsg;
            if (m_cMailbox.Get( &dwMsg, MAILBOX_TIMEOUT)) {
                // message read succeeded...
                printf( "Mailbox reader thread id 0x%08x read %d.\n",
                    GetCurrentThreadId(), dwMsg);
            }
            else {
                // message read failed...
                printf( "Mailbox reader thread id 0x%08x timed out.\n",
                    GetCurrentThreadId(), dwMsg);
            }
        }
        return 0;
    }

    void Stop(void) {
        m_bRun = FALSE;
    }
};

class CMclMailboxWriter : public CMclThreadHandler {
private:
    CMclMailbox m_cMailbox;
    BOOLEAN m_bRun;

public:
    // class constructor initializes the mailbox object...
    CMclMailboxWriter( LPCTSTR lpszName) :
        m_cMailbox( MAILBOX_DEPTH, MAILBOX_MSG_SIZE, lpszName) {
            m_bRun = TRUE;
            return;
    }

    virtual unsigned ThreadHandlerProc(void) {
        DWORD dwMsg = 0;
        while (m_bRun) {
            if (m_cMailbox.Post( &dwMsg, MAILBOX_TIMEOUT)) {
                // message post succeeded...
                printf( "Mailbox writer thread id 0x%08x posted %d.\n",
                    GetCurrentThreadId(), dwMsg);

                // increment the message...
                dwMsg++;
            }
            else {
                // message post failed...
                printf( "Mailbox writer thread id 0x%08x timed out, will repost %d.\n",
                    GetCurrentThreadId(), dwMsg);
            }
        }
        return 0;
    }

    void Stop(void) {
        m_bRun = FALSE;
    }
};

int main( int argc, char *argv[]) {
    // create and initialize the mailbox writer handler...
    CMclMailboxWriter cWriter(MAILBOX_NAME);

    // start the mailbox writer thread...
    CMclThreadAutoPtr apWriterThread = new CMclThread(&cWriter);

    // create and initialize the mailbox reader handler...
    CMclMailboxReader cReader(MAILBOX_NAME);

    // start the mailbox reader thread...
    CMclThreadAutoPtr apReaderThread = new CMclThread(&cReader);

    // let them run for a little while...
    Sleep(50);

    // stop the writer thread...
    cWriter.Stop();

    // stop the reader thread...
    cReader.Stop();

    // wait until both threads have exited...
    apWriterThread->WaitForTwo( *apReaderThread, TRUE, INFINITE);

    // everything cleans up automatically!

    return 0;
}


⌨️ 快捷键说明

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