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

📄 alertable.cpp

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

class SecondaryThread : public CMclThreadHandler {
private:
    CMclEvent m_ceStop;
    CMclQueue<int> *m_pcqNumbers;

public:
    SecondaryThread() : m_ceStop(TRUE), m_pcqNumbers(NULL) {
        // create the queue...
        m_pcqNumbers = new CMclQueue<int>;
    };

    ~SecondaryThread() {
        // destroy the queue...
        delete m_pcqNumbers;
    };
    
    void Put( int n) {
        m_pcqNumbers->Put(n);
    };

    void Stop(void) {
        m_ceStop.Set();
    };

    unsigned ThreadHandlerProc() {
        int total = 0;
        int num;

        while (TRUE) {
            if (m_pcqNumbers->Get( num, INFINITE, &m_ceStop))
                total += num;
            else
                break;
        }

        printf( "The total is %d.\n", total);
        printf( "Secondary thread exiting.\n");
        return 0;
    };
};

int main( int argc, char *argv) {
    // create the secondary thread handler...
    SecondaryThread chSecond;

    // create the secondary thread...
    CMclThreadAutoPtr apSecond = new CMclThread( &chSecond);

    // put some numbers in the queue...
    // add up our own total as a check...
    int total = 0;
    for (int n = 1; n < 20; n++) {
        total += n;
        chSecond.Put(n);
    }

    // wait a second to give the secondary thread a chance to run...
    Sleep(1000);

    // tell the secondary thread to exit by using the
    // thread handler object to set the stop event...
    chSecond.Stop();

    // wait for the secondary thread to complete...
    apSecond->Wait(INFINITE);

    // done...
    printf( "Primary thread exiting, total should be %d.\n", total);
    return 0;
}

⌨️ 快捷键说明

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