亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? airline.cpp

?? window下的多線程編程參考書。值得一讀
?? CPP
字號:
#include <stdio.h>
#include "CMcl.h"

#define NUMBER_AGENTS       10
#define NUMBER_OF_SEATS     40

class SeatDataBase {
private:
    // track the status of the seats...
    int m_nSeats;
    int m_nSeatsAvailable;
    BOOL m_bSeatAvailable[NUMBER_OF_SEATS];

    // coordinate reader/writer access to the database...
    
    // number of current readers...
    int m_nNumberReaders;

    // mutex for writers and for modifying m_nNumberReaders
    // and m_ceNoReaders...
    CMclMutex m_cmNoWriters;
    
    // event which is signaled when there are no readers...
    CMclEvent m_ceNoReaders;

public:
    SeatDataBase() : m_cmNoWriters(), m_ceNoReaders(TRUE,TRUE) {
        m_nNumberReaders = 0;
        m_nSeatsAvailable = m_nSeats = NUMBER_OF_SEATS;
        for (int i = 0; i < NUMBER_OF_SEATS; i++)
            m_bSeatAvailable[i] = TRUE;
    };

    int NumberOfSeatsAvailable(void) {
        return m_nSeatsAvailable;
    };

    int NumberOfSeats(void) {
        return m_nSeats;
    };

    BOOL CheckSeat( int nSeat) {
        if (nSeat > m_nSeats)
            return FALSE;
        else
            return m_bSeatAvailable[nSeat];
    };

    BOOL ReserveSeat( int nSeat) {
        if (nSeat > m_nSeats) {
            return FALSE;
        }
        else if (m_bSeatAvailable[nSeat] == FALSE) {
            return FALSE;
        }
        else {
            // simulate that a database update takes some time...
            // this will also cause more contention for access to
            // the database, which is what we want to simulate...
            Sleep(250);

            // reserve the seat...
            m_bSeatAvailable[nSeat] = FALSE;
            m_nSeatsAvailable--;
            return TRUE;
        }
    };

    void GrantReaderAccess(void) {
        // grab the mutex to modify the state of
        // internal objects...
        m_cmNoWriters.Wait(INFINITE);

        // increase the reader count...
        m_nNumberReaders++;

        // now there are NOT no readers...
        m_ceNoReaders.Reset();

        // release the mutex...
        m_cmNoWriters.Release();
    };

    void ReleaseReaderAccess(void) {
        // grab the mutex to modify the state of
        // internal objects...
        m_cmNoWriters.Wait(INFINITE);

        // decrease the reader count...
        if (--m_nNumberReaders == 0) {
            // we must set the no readers event...
            m_ceNoReaders.Set();
        }

        // release the mutex...
        m_cmNoWriters.Release();
    };

    void GrantWriterAccess(void) {
        // we must grab BOTH the no readers event and
        // the no writers mutex...
        m_cmNoWriters.WaitForTwo( m_ceNoReaders, TRUE, INFINITE);
    };

    void ReleaseWriterAccess(void) {
        // now that we are done writing we
        // can release the no writers mutex...
        m_cmNoWriters.Release();
    };
};

class ReservationAgent : public CMclThreadHandler {
private:
    int m_nId;
    SeatDataBase *m_psdb;
    BOOL m_bContinue;

public:
    ReservationAgent(int nId, SeatDataBase *psdb) : m_nId(nId), m_psdb( psdb) {
        m_bContinue = TRUE;
        return;
    };

    BOOL ReserveSeat(int nSeat) {
        m_psdb->GrantWriterAccess();
        BOOL bStatus = m_psdb->ReserveSeat(nSeat);
        m_psdb->ReleaseWriterAccess();
        return bStatus;
    };

    int FindSeatInFront(int iStartingSeat) {
        int nSeat;
        
        m_psdb->GrantReaderAccess();
        
        for (int i = iStartingSeat; i < m_psdb->NumberOfSeats(); i++) {
            if (m_psdb->CheckSeat(i) == TRUE) {
                nSeat = i;
                break;
            }
        }
        if (i == m_psdb->NumberOfSeats())
            nSeat = -1;

        m_psdb->ReleaseReaderAccess();

        return nSeat;
    };

    int FindSeatInRear(int iStartingSeat) {
        int nSeat;
        
        m_psdb->GrantReaderAccess();
        
        for (int i = iStartingSeat; i >= 0; i--) {
            if (m_psdb->CheckSeat(i) == TRUE) {
                nSeat = i;
                break;
            }
        }
        if (i == -1)
            nSeat = -1;

        m_psdb->ReleaseReaderAccess();

        return nSeat;
    };

    unsigned ThreadHandlerProc(void) {
        // intialize random numbers...
        srand(GetCurrentThreadId());

        printf( "Agent #%d opening ticket counter.\n", m_nId);
        
        while (m_bContinue) {
            // customers arrive at random times...
            Sleep(rand() % 1000);
            BOOL bCustomerSeated = FALSE;

            // some customers want a seat in the front, others
            // want to sit in the back...
            BOOL bPreferFront = rand() & 1;

            // seat to start looking at...
            int iStartingSeat;
            if (bPreferFront)
                iStartingSeat = 0;
            else
                iStartingSeat = m_psdb->NumberOfSeats() - 1;

            // work with this customer until they are seated or
            // there are no seats left...
            while (!bCustomerSeated) {
                // are there any seats available? if not the customer
                // will be turned away...
                if (m_psdb->NumberOfSeatsAvailable() == 0) {
                    printf( "Agent #%d turned away customer, plane full.\n", m_nId);

                    // agents will continue to man their counters even when the
                    // seats are all taken...
                    break;
                }

                int nSeat;
                if (bPreferFront)
                    nSeat = FindSeatInFront(iStartingSeat);
                else
                    nSeat = FindSeatInRear(iStartingSeat);

                // if the seat number is -1, no seat could be found, the
                // plane must have filled up...
                if (nSeat == -1) {
                    printf( "Agent #%d turned away customer, unable to find seat.\n", m_nId);
                    break;
                }

                // ask customer if this seat is acceptable...
                // since this is a simulation, half of the time
                // the customer will not like the seat...
                if (rand() & 1) {
                    printf( "Agent #%d's customer did not like seat #%d, trying again.\n", m_nId, nSeat);
                    if (bPreferFront)
                        iStartingSeat++;
                    else
                        iStartingSeat--;
                }
                else {
                    // we found a seat, try to reserve it...
                    if (ReserveSeat(nSeat)) {
                        printf( "Agent #%d reserved seat #%d, %d seats left.\n", m_nId, nSeat, m_psdb->NumberOfSeatsAvailable());
                        bCustomerSeated = TRUE;
                    }
                    else {
                        // unable to reserve that seat, some other agent must have
                        // taken it, try again...
                        printf( "Agent #%d unable to reserve seat #%d, trying again.\n", m_nId, nSeat);
                    }
                }
            }
        }

        printf( "Agent #%d closing ticket counter.\n", m_nId);
        return 0;
    };

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

class ReservationAgentThread : public CMclThread {
private:
    ReservationAgent *m_pAgent;

    // internal constructor...
    ReservationAgentThread(ReservationAgent *pAgent) : m_pAgent(pAgent), CMclThread(pAgent) {
        return;
    };

    // destructor which cleans up internal thread handler object...
    ~ReservationAgentThread() {
        delete m_pAgent;
    };

public:
    // users of this class create agents with this static function...
    static ReservationAgentThread *CreateReservationAgentThread( int nId, SeatDataBase *psdb) {
        ReservationAgent *pAgent = new ReservationAgent(nId, psdb);
        return new ReservationAgentThread(pAgent);
    };

    // stop the agent with this...
    void Stop(void) {
        m_pAgent->Stop();
    };
};

int main(int argc, char *argv[]) {
    int i;

    // data base for seat reservations...
    SeatDataBase sdb;

    // ticket agent thread auto pointer array...
    CMclDerivedAutoPtr<ReservationAgentThread> apReservationAgentThreads[NUMBER_AGENTS];

    // waitable collection to synchronize shutdown...
    CMclWaitableCollection collection;

    // create the ticket agents and add them to the collection...
    for (i = 0; i < NUMBER_AGENTS; i++) {
        apReservationAgentThreads[i] = ReservationAgentThread::CreateReservationAgentThread( i, &sdb);
        collection.AddObject(*apReservationAgentThreads[i]);
    }

    // let the simulation run for a while...
    Sleep(5000);

    // tell the agents to stop selling tickets...
    for (i = 0; i < NUMBER_AGENTS; i++) {
        apReservationAgentThreads[i]->Stop();
    };

    // wait for all of the agents to stop...
    collection.Wait( TRUE, INFINITE);

    // all done...
    printf( "All done, exiting.\n");
    return 0;
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲日本韩国一区| 免费xxxx性欧美18vr| 91麻豆精品视频| 日本午夜精品一区二区三区电影| 欧美群妇大交群的观看方式| 麻豆精品一区二区三区| 久久精品亚洲国产奇米99| 色偷偷成人一区二区三区91 | 日本道在线观看一区二区| 天天综合色天天| 亚洲色图.com| 欧美国产精品中文字幕| 欧美疯狂性受xxxxx喷水图片| 成人网男人的天堂| 九一九一国产精品| 91啪在线观看| 亚洲日本乱码在线观看| 日韩一区欧美小说| 国产午夜精品在线观看| 欧美精品一区二区在线播放| 精品污污网站免费看| 91日韩一区二区三区| 国产精品69久久久久水密桃| 麻豆精品一区二区三区| 五月天久久比比资源色| 天堂影院一区二区| 日韩av一级电影| 美女精品一区二区| 激情综合色播五月| 国产精品影视在线观看| 成人免费三级在线| 99久久精品免费看国产| 欧美在线观看一区| 日韩视频在线你懂得| 精品99999| 日本一二三不卡| 亚洲一区免费观看| 精品一区二区三区不卡| 成人一区二区三区视频在线观看| av午夜一区麻豆| 日韩亚洲欧美成人一区| 国产女主播视频一区二区| 亚洲欧美精品午睡沙发| 午夜视频一区二区三区| 韩国一区二区三区| 欧美三级韩国三级日本三斤| 日韩欧美一卡二卡| 亚洲综合一区二区| 国产成人综合在线观看| 色猫猫国产区一区二在线视频| 在线综合亚洲欧美在线视频| 久久久久9999亚洲精品| 亚洲一区国产视频| 色综合视频一区二区三区高清| 精品国产青草久久久久福利| 亚洲美女屁股眼交3| 成人免费看的视频| 久久亚洲精品国产精品紫薇| 偷拍自拍另类欧美| 欧美日韩精品三区| 亚洲美女在线一区| 99精品国产99久久久久久白柏 | 国产精品一色哟哟哟| 欧美日韩亚洲综合| 亚洲18影院在线观看| 色吊一区二区三区| 亚洲欧美日韩国产手机在线| 成人午夜电影久久影院| 国产日韩欧美制服另类| 国产精品一区在线观看乱码| 欧美国产日本视频| 9l国产精品久久久久麻豆| 国产精品入口麻豆原神| 一本大道久久a久久综合婷婷| 一区二区中文字幕在线| 99riav一区二区三区| 一区二区三区四区蜜桃| 欧美日韩激情在线| 国产高清亚洲一区| 亚洲六月丁香色婷婷综合久久| 一本久道中文字幕精品亚洲嫩| 亚洲一区二区欧美日韩| 欧美一区欧美二区| 99久久精品免费| 日本大胆欧美人术艺术动态| 2欧美一区二区三区在线观看视频 337p粉嫩大胆噜噜噜噜噜91av | 成人综合在线视频| 午夜国产精品一区| 国产丝袜欧美中文另类| 欧美日韩午夜影院| 国产一区激情在线| 亚洲成av人综合在线观看| 久久久久久久久99精品| 欧美美女一区二区三区| 成人国产精品视频| 久久激情综合网| 日韩精品亚洲专区| 亚洲欧美日本韩国| 国产精品美女久久久久aⅴ| 国产欧美一区二区精品婷婷| 色综合中文字幕国产 | 亚洲午夜精品17c| 国产精品网曝门| 久久这里只有精品首页| 日韩欧美国产一区二区三区| 在线一区二区三区四区五区 | 久久久国产综合精品女国产盗摄| 欧美日韩一区二区三区免费看 | 亚洲黄色尤物视频| 亚洲国产美国国产综合一区二区| 国产精品天干天干在观线| 国产午夜精品一区二区| 国产午夜精品在线观看| 日本一区二区三区视频视频| 亚洲精品在线电影| 中文字幕不卡一区| 久久精子c满五个校花| 亚洲国产精品av| 成人免费在线观看入口| 一区二区三区高清| 亚洲成人精品影院| 日韩va欧美va亚洲va久久| 蜜臀精品一区二区三区在线观看 | 国内不卡的二区三区中文字幕| 99久久777色| 在线不卡欧美精品一区二区三区| 日韩三级中文字幕| 国产精品久久久久国产精品日日 | 一区二区三区精品在线观看| 午夜成人免费视频| 91在线精品一区二区三区| 色猫猫国产区一区二在线视频| 久久久无码精品亚洲日韩按摩| 精品伦理精品一区| 中文字幕日韩欧美一区二区三区| 一二三区精品福利视频| 国产精品一区二区免费不卡| 91福利视频网站| 国产精品色哟哟网站| 国产在线一区观看| 欧美在线短视频| 国产精品理论在线观看| 国产一区久久久| 精品少妇一区二区三区| 午夜精品福利久久久| 色美美综合视频| 亚洲国产精品一区二区久久恐怖片| 成人18精品视频| 中文字幕中文字幕一区二区| 国产成人免费视频一区| 欧美变态tickle挠乳网站| 婷婷六月综合网| 欧美成人综合网站| 国产米奇在线777精品观看| 久久这里只精品最新地址| 极品尤物av久久免费看| 亚洲风情在线资源站| 97精品国产97久久久久久久久久久久| 日韩欧美一级二级三级| 国产成人在线观看| 日韩理论电影院| 色综合天天视频在线观看| 日韩成人一区二区| 26uuu精品一区二区| 国产精品一区二区三区四区| 中文在线资源观看网站视频免费不卡| 夫妻av一区二区| 午夜精品久久久久久久99水蜜桃| 久久天堂av综合合色蜜桃网| 91色九色蝌蚪| 国产毛片一区二区| 亚洲免费在线视频一区 二区| 在线免费观看一区| 精品一区二区三区不卡| 亚洲情趣在线观看| 日韩欧美在线网站| 91色在线porny| 成人午夜av影视| 日本成人超碰在线观看| 亚洲男人电影天堂| 久久影视一区二区| 欧美日韩大陆在线| 91麻豆福利精品推荐| 国产毛片一区二区| 精品在线观看视频| 丝袜诱惑亚洲看片| 亚洲女人****多毛耸耸8| 久久亚洲精品国产精品紫薇| 日韩欧美中文字幕精品| 欧美日韩国产天堂| 欧美日韩国产一区二区三区地区| 成人av资源网站| av男人天堂一区| 99久久99久久精品免费看蜜桃| 成a人片亚洲日本久久| 国产91丝袜在线观看| 国产99一区视频免费| 成人av免费在线观看| 色又黄又爽网站www久久| 在线观看国产日韩|