2016-12-29 2 views
0

Я начал с основной-agents.cpp здесь Asynchronous Agents, но я хотел, чтобы проверить петлю, так что я сделалC++ Асинхронные агенты Странное поведение

class agent1 : public agent 
{ 
public: 
    explicit agent1(ISource<int>& source, ITarget<string>& target) 
     : _source(source) 
     , _target(target) 
    { 
    } 

protected: 
void run() 
{ 
    std::cout << "type: "; 
    std::string a; 
    std::getline(std::cin, a); 

    // send the request 
    std::cout << "agent1: sending request... " << endl; 

    send(_target, a); 

    // Read the response 
    int response = receive(_source); 

    std::cout << "agent1: received '" << response << "'." << endl; 

    // move the agent to the finished state. 
    done(); 
    } 

private: 
    ISource<int>& _source; 
    ITarget<string >& _target; 
}; 

и для agent2

class agent2 : public agent 
{ 
public: 
    explicit agent2(ISource<string>& source, ITarget<int>& target) 
     : _source(source) 
     , _target(target) 
    { 
    } 

protected: 
void run() 
{ 
    // read the request 
    string request = receive(_source); 

    std::cout << "agent2: received '" << request << "'." << std::endl; 

    // send the response 
    std::cout << "agent2: sending response..." << std::endl; 

    code += 1; 
    send(_target, code); 

    // move the agent to the finished state 
    done(); 

public: 
    int code; 
private: 
    ISource<string>& _source; 
    ITarget<int>& _target; 
}; 

и основной работает агентов, как

int oldcode = 0; 
    while (oldcode < 10) 
    { 
     agent1 first_agent(buffer2, buffer1); 
     agent2 second_agent(buffer1, buffer2); 
     second_agent.code = oldcode; 

     // Step 3: start the agents. The runtime calls the run method on each agent. 
     first_agent.start(); 
     second_agent.start(); 

     // Step 4: wait for both agents to finish 
     agent::wait(&first_agent); 
     agent::wait(&second_agent); 
     oldcode = second_agent.code; 
     std::cout << "In Step 2 loop, oldcode: " << oldcode << std::endl; 
    } 

Самое смешное, что я получаю этот выход

type: a 
agent1: sending request... 
agent2: received 'a'. 
agent2: sending response... 
agent1: received '1'. 
In Step 2 loop, oldcode: 1 
type: b 
agent1: sending request... 
agent1: received '1'. 
agent2: received 'b'. 
agent2: sending response... 
In Step 2 loop, oldcode: 2 
type: c 
agent1: sending request... 
agent1: received '2'. 
agent2: receivede 'c'. 
agent2: sending response... 
In Step 2 loop, oldcode: 3 

Обратите внимание, что агент 1 сообщал о получении «1» дважды в начале с получением последовательности после ответа на «b». Это не происходит нигде и не согласуется.

Что-то не так с моими асинхронными агентами цикла таким образом?

ответ

0

Я думаю, что получил. https://msdn.microsoft.com/en-us/library/dd728068(v=vs.120).aspx «В отличие от объекта unbounded_buffer, функция приема не удаляет сообщение из объекта overwrite_buffer. Если потребитель читает из буфера сообщений более одного раза, прежде чем производитель перезаписывает это сообщение, получатель получает одно и то же сообщение каждый раз «.

Смежные вопросы