-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromise.cpp
More file actions
33 lines (32 loc) · 1.14 KB
/
Copy pathpromise.cpp
File metadata and controls
33 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <future>
#include <vector>
#include <iostream>
/*这里 promise是和future相关联的
future阻塞等待数据,promise调用
set_value方法,就表示数据设置成功
future就可以得到数据了。
下面的东西过于复杂,不好实现,就直接抄的书
一个线程阻塞在future上等待数据,另一个线程
用相对应得promise上调用set_value使得
future就绪得到set_value的value数据*/
void process_connection(connection_set &connections)
{
while(!done(connections))
{
for(auto connection = connections.begin(),end = connections.end(); connection != end;++connection)
{
if(connection->has_incoming_data())
{
data_packet data = connection->incoming();
std::promise<payload_type> &p = connection->get_promise(data.id);
p.set_value(data.payload);
}
if(connection->has_outgoing_data())
{
outgoing_packet data = connection->top_of_outging_queue();
connecttion->send(data.payload);
data.promise.set_value(true);
}
}
}
}