>  기사  >  백엔드 개발  >  C++ 솔루션: 다중 스레드 동기화의 전형적인 사례: 생산자-소비자 문제

C++ 솔루션: 다중 스레드 동기화의 전형적인 사례: 생산자-소비자 문제

php是最好的语言
php是最好的语言원래의
2018-08-06 13:56:383388검색

Wikipedia에서 복사됨:

생산자-소비자 문제(영어: 생산자-소비자 문제)는 Bounded-buffer 문제(영어: Bounded-buffer 문제)라고도 알려져 있으며 다중 스레드 동기화의 전형적인 사례입니다. 문제 . 이 질문은 고정 크기 버퍼를 공유하는 두 스레드(소위 "생산자"와 "소비자")가 실제로 실행될 때 발생하는 문제를 설명합니다. 생산자의 주요 역할은 일정량의 데이터를 생성하여 버퍼에 넣은 후 프로세스를 반복하는 것입니다. 동시에 소비자도 버퍼의 데이터를 소비하고 있습니다. 이 문제의 핵심은 버퍼가 가득 찼을 때 생산자가 데이터를 추가하지 않고, 버퍼가 비어 있을 때 소비자가 데이터를 소비하지 않도록 하는 것입니다.

이 문제를 해결하려면 버퍼가 가득 찼을 때 생산자를 절전 모드로 전환해야 합니다(또는 간단히 데이터를 포기해야 함). 다음에 소비자가 버퍼의 데이터를 소비할 때 생산자가 깨어나서 버퍼를 시작할 수 있습니다. 데이터를 추가합니다. 마찬가지로, 버퍼가 비어 있으면 소비자를 절전 모드로 전환하고 생산자가 버퍼에 데이터를 추가할 때까지 기다린 다음 소비자를 깨울 수도 있습니다.

이 기사에서는 ItemRepository 클래스를 사용하여 좌표로 표시되는 배열과 두 개의 순환 큐가 포함된 제품 창고를 나타내고 std::mutex 멤버를 사용하여 한 번에 하나의 스레드에서만 읽고 쓸 수 있도록 합니다( 인쇄된 메시지가 한 줄씩 전송되고 유휴 상태일 때 뮤텍스 ╮(╯▽╰)╭)도 차용되도록 하기 위해 두 개의 std::condition_variables는 만족되지 않은 대기열의 상태를 나타냅니다. 비어 있으므로 생산 중에 대기열이 만족되지 않고 소비됩니다.

#pragma once
#include <chrono>//std::chrono
#include <mutex>//std::mutex,std::unique_lock,std::lock_guard
#include <thread>//std::thread
#include <condition_variable>//std::condition_variable
#include <iostream>//std::cout,std::endl
#include <map>//std::map
namespace MyProducerToConsumer {
    static const int gRepositorySize = 10;//total size of the repository
    static const int gItemNum = 97;//number of products to produce
    std::mutex produce_mtx, consume_mtx;//mutex for all the producer thread or consumer thread
    std::map<std::thread::id, int> threadPerformance;//records of every thread&#39;s producing/consuming number
    struct ItemRepository {//repository class
        int m_ItemBuffer[gRepositorySize];//Repository itself (as a circular queue)
        int m_ProducePos;//rear position of circular queue
        int m_ConsumePos;//head position of circular queue
        std::mutex m_mtx;//mutex for operating the repository
        std::condition_variable m_RepoUnfull;//indicating that this repository is unfull(then producers can produce items)
        std::condition_variable m_RepoUnempty;//indicating that this repository is unempty(then consumers can produce items)
    }gItemRepo;

    void ProduceItem(ItemRepository *ir, int item) {
        std::unique_lock <std::mutex>ulk(ir->m_mtx);
        while ((ir->m_ProducePos + 1) % gRepositorySize == ir->m_ConsumePos) {//full(spare one slot for indicating)
            std::cout << "Reposity is full. Waiting for consumers..." << std::endl;
            ir->m_RepoUnfull.wait(ulk);//unlocking ulk and waiting for unfull condition
        }
        //when unfull
        ir->m_ItemBuffer[ir->m_ProducePos++] = item;//procude and shift
        std::cout << "Item No." << item << " produced successfully by "
            <<std::this_thread::get_id()<<"!" << std::endl;
        threadPerformance[std::this_thread::get_id()]++;
        if (ir->m_ProducePos == gRepositorySize)//loop
            ir->m_ProducePos = 0;
        ir->m_RepoUnempty.notify_all();//item produced, so it&#39;s unempty; notify all consumers
    }

    int ConsumeItem(ItemRepository *ir) {
        std::unique_lock<std::mutex>ulk(ir->m_mtx);
        while (ir->m_ConsumePos == ir->m_ProducePos) {//empty
            std::cout << "Repository is empty.Waiting for producing..." << std::endl;
            ir->m_RepoUnempty.wait(ulk);
        }
        int item = ir->m_ItemBuffer[ir->m_ConsumePos++];
        std::cout << "Item No." << item << " consumed successfully by "
            <<std::this_thread::get_id()<<"!" << std::endl;
        threadPerformance[std::this_thread::get_id()]++;
        if (ir->m_ConsumePos == gRepositorySize)
            ir->m_ConsumePos = 0;
        ir->m_RepoUnfull.notify_all();//item consumed, so it&#39;s unempty; notify all consumers
        return item;
    }

    void ProducerThread() {
        static int produced = 0;//static variable to indicate the number of produced items
        while (1) {
            std::this_thread::sleep_for(std::chrono::milliseconds(10));//sleep long enough in case it runs too fast for other threads to procude
            std::lock_guard<std::mutex>lck(produce_mtx);//auto unlock when break
            produced++;
            if (produced > gItemNum)break;
            gItemRepo.m_mtx.lock();
            std::cout << "Producing item No." << produced << "..." << std::endl;
            gItemRepo.m_mtx.unlock();
            ProduceItem(&gItemRepo, produced);
        }
        gItemRepo.m_mtx.lock();
        std::cout << "Producer thread " << std::this_thread::get_id()
            << " exited." << std::endl;
        gItemRepo.m_mtx.unlock();
    }

    void ConsumerThread() {
        static int consumed = 0;
        while (1) {
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            std::lock_guard<std::mutex>lck(consume_mtx);
            consumed++;
            if (consumed > gItemNum)break;
            gItemRepo.m_mtx.lock();
            std::cout << "Consuming item available..." << std::endl;
            gItemRepo.m_mtx.unlock();
            ConsumeItem(&gItemRepo);
        }
        gItemRepo.m_mtx.lock();
        std::cout << "Consumer thread " << std::this_thread::get_id()
            << " exited." << std::endl;
        gItemRepo.m_mtx.unlock();
    }

    void InitItemRepository(ItemRepository* ir) {
        ir->m_ConsumePos = 0;
        ir->m_ProducePos = 0;
    }

    void Run() {
        InitItemRepository(&gItemRepo);
        std::thread thdConsume[11];
        std::thread thdProduce[11];
        for (auto& t : thdConsume)t = std::thread(ConsumerThread);
        for (auto& t : thdProduce)t = std::thread(ProducerThread);
        for (auto& t : thdConsume)t.join();
        for (auto& t : thdProduce)t.join();
        for (auto& iter : threadPerformance)cout << iter.first << ":" << iter.second << endl;
    }
}

관련 기사:

Java 생산자와 소비자의 자세한 예

Java 다중 스레드 동시 협업 생산자-소비자 디자인 패턴

위 내용은 C++ 솔루션: 다중 스레드 동기화의 전형적인 사례: 생산자-소비자 문제의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.