-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path41_condition_variable.cpp
More file actions
92 lines (87 loc) · 2.84 KB
/
41_condition_variable.cpp
File metadata and controls
92 lines (87 loc) · 2.84 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// uses boost::condition_variable to coordinate two threads - one of them
// writing to a global variable and one of them reading from it;
// program must be terminated with CTRL+C as it uses infinite loops in reader threads
#define BOOST_THREAD_VERSION 4
#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <string>
#include <sstream>
#include <iostream>
#include <functional>
std::string get_robotstxt(const std::string &host)
{
using namespace boost::asio;
io_service ioservice;
ip::tcp::resolver resolver(ioservice);
ip::tcp::resolver::query query(host, "http");
auto it = resolver.resolve(query);
ip::tcp::socket socket(ioservice);
socket.connect(*it);
std::string request = "GET /robots.txt HTTP/1.1\r\nHost: " + host + "\r\n\r\n";
write(socket, buffer(request));
streambuf response;
boost::system::error_code ec;
read(socket, response, transfer_all(), ec);
if (ec == error::eof)
{
std::ostringstream os;
os << &response;
std::string s = os.str();
std::size_t idx = s.find("\r\n\r\n");
if (idx != std::string::npos)
s.erase(0, idx + 4);
return s;
}
return "";
}
std::string line;
boost::mutex line_mutex;
// condition variable for global variable line above
boost::condition_variable line_cond;
void store_lines_from_robotstxt(const std::string &host)
{
std::string robotstxt = get_robotstxt(host);
std::istringstream is(robotstxt);
std::string s;
boost::unique_lock<boost::mutex> lock(line_mutex);
while (std::getline(is, s))
{
line = s;
// notify_all() sends a notification to all threads currently
// waiting on the condition variable to wake up and resume
line_cond.notify_all();
// wait() pauses the thread until a notification is sent;
// wait() may only be called with a locked mutex; wait()
// automatically unlocks the mutex; the mutex is automatically
// locked again when wait() returns; the while-loop is used to
// protect us from spurious wakeups (see https://en.wikipedia.org/wiki/Spurious_wakeup)
while (!line.empty())
line_cond.wait(lock);
}
}
void output_lines()
{
boost::unique_lock<boost::mutex> lock(line_mutex);
for (;;)
{
// wait() pauses the thread until a notification is sent;
// wait() may only be called with a locked mutex; wait()
// automatically unlocks the mutex; the mutex is automatically
// locked again when wait() returns; the while-loop is used to
// protect us from spurious wakeups (see https://en.wikipedia.org/wiki/Spurious_wakeup)
while (line.empty())
line_cond.wait(lock);
std::cout << line << '\n';
line.clear();
// notify_all() sends a notification to all threads currently
// waiting on the condition variable to wake up and resume
line_cond.notify_all();
}
}
int main()
{
boost::thread_group group;
group.create_thread(std::bind(store_lines_from_robotstxt, "theboostcpplibraries.com"));
group.create_thread(output_lines);
group.join_all();
}