簡體   English   中英

使用c ++ 11實現使用期貨和承諾的雙緩沖

[英]Implementing Double Buffering using Futures and Promises using c++11

我開始學習多線程,並遇到了期望和承諾,通過共享資源同步線程。 因此,我想到使用Futures and Promises(單一生產者和單一消費者)實現着名的雙緩沖問題。 我所考慮的基本方法是:

ProducerThread:

loop:    
    locks_buffer1_mutex    
    fills_buffer1   
    unlocks_buffer1_mutex    
    passes number 1 to Consumer thread using promise.setvalue()    
    locks_buffer2_mutex    
    fills_buffer2   
    unlocks_buffer2_mutex    
    passes number 2 to Consumer thread using promise.setvalue()
back_to_loop

ConsumerThread:

loop:
   wait_for_value_from_promise
   switch
      case 1:
         lock_buffer1_mutex
          process(buffer1)
         unlock_buffer1_mutex
         print_values
         break
       case 2:
         lock_buffer2_mutex
         process(buffer2)
         unlock_buffer2_mutex
         print_values
         break
back_to_loop

這是代碼:

#include <iostream>
#include <thread>
#include <vector>
#include <future>
#include <mutex>
#include <iterator>


std::mutex print_mutex;
std::mutex buffer1_mutex;
std::mutex buffer2_mutex;

std::vector<int> buffer1;
std::vector<int> buffer2;

bool notify;


void DataAcquisition(std::promise<int> &p)
{
    std::this_thread::sleep_for(std::chrono::seconds(2));
    while(true)
    {
        {
            std::lock_guard<std::mutex> buff1_lock(buffer1_mutex);
            for(int i=0;i<200;i++)
            {
                buffer1.push_back(i);
            }
        }
        p.set_value(1);
        {
            std::lock_guard<std::mutex> buff2_lock(buffer2_mutex);
            for(int i=0;i<200;i++)
            {
                buffer2.push_back(199-i);
            }
        }
        p.set_value(2);
    }
}

void DataExtraction(std::future<int> &f)
{
    std::vector<int>::const_iterator first,last;
    std::vector<int> new_vector;
    std::ostream_iterator<int> outit(std::cout, " ");

    while(true)
    {
        int i = f.get();
        std::cout << "The value of i is :" << i << std::endl;
        switch(i)
        {
            case 1:
                {
                    std::lock_guard<std::mutex> buff1_lock(buffer1_mutex);
                    first = buffer1.begin();
                    last = first + 10;
                }
                new_vector = std::vector<int>(first,last);
                {
                    std::lock_guard<std::mutex> print_lock(print_mutex);
                    std::copy(new_vector.begin(),new_vector.end(),outit);
                }
                break;
              case 2:
                {
                    std::lock_guard<std::mutex> buff2_lock(buffer2_mutex);
                    first = buffer2.begin();
                    last = first + 10;
                }
                new_vector = std::vector<int>(first,last);
                {
                    std::lock_guard<std::mutex> print_lock(print_mutex);
                    std::copy(new_vector.begin(),new_vector.end(),outit);
                }
                break;
           }
    }
}

int main()
{
    std::promise<int> p;
    std::future<int> f = p.get_future();


    std::thread thread1(DataAcquisition,std::ref(p));
    std::thread thread2(DataExtraction,std::ref(f));

    thread1.join();
    thread2.join();

    return 0;
}

當我執行這段代碼時,我遇到了他的giagntic問題,我完全沒有意識到這一點

terminate called after throwing an instance of 'std::future_error' terminate called recursively
  what(): 0 1 2 3 4 5 6 7 8 9 Promise already satisfied
Press <RETURN> to close the window

我已經google了這個錯誤,建議在鏈接和編譯時鏈接-lpthread切換。 但無法解決問題。

請幫幫我,我哪里錯了..

您不能再為一次promise調用set_value ,這可以通過以下代碼說明:

#include <future>

int main() {
    std::promise<int> p;
    p.set_value(1);
    p.set_value(2); // Promise already satisfied
}

你必須尋找另一種方法。 例如,你可以使用兩個std :: condition_variable s - 在producer中設置它們,並在consumer中等待它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM