| 1 | #include <iostream>
|
|---|
| 2 | #include <string>
|
|---|
| 3 | #include <boost/thread.hpp>
|
|---|
| 4 | #include <boost/asio.hpp>
|
|---|
| 5 | #include <boost/bind.hpp>
|
|---|
| 6 | using namespace std;
|
|---|
| 7 | using namespace boost;
|
|---|
| 8 | using namespace boost::asio;
|
|---|
| 9 | using namespace boost::asio::local;
|
|---|
| 10 |
|
|---|
| 11 | void accept(thread::id id)
|
|---|
| 12 | {
|
|---|
| 13 | cout << "accept from " << this_thread::get_id() << ", async_accept from " << id << endl;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | void thread_run(shared_ptr<io_service> &io, shared_ptr<stream_protocol::acceptor> &acceptor)
|
|---|
| 17 | {
|
|---|
| 18 | shared_ptr<io_service::work> work(new io_service::work(*io));
|
|---|
| 19 | cout << "start from " << this_thread::get_id() << endl;
|
|---|
| 20 | stream_protocol::socket sock(*(io.get()));
|
|---|
| 21 | acceptor->async_accept(sock, bind(&accept, this_thread::get_id()));
|
|---|
| 22 | io->run();
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | int main()
|
|---|
| 26 | {
|
|---|
| 27 | ::unlink("/tmp/fcgi_test.sock");
|
|---|
| 28 | shared_ptr<io_service> io_1(new io_service()), io_2(new io_service());
|
|---|
| 29 | stream_protocol::endpoint ep("/tmp/fcgi_test.sock");
|
|---|
| 30 | shared_ptr<stream_protocol::acceptor> acceptor(new stream_protocol::acceptor(*(io_1.get()), ep, true));
|
|---|
| 31 | shared_ptr<thread> thread_1(new thread(&thread_run, io_1, acceptor));
|
|---|
| 32 | shared_ptr<thread> thread_2(new thread(&thread_run, io_2, acceptor));
|
|---|
| 33 | thread_1->join();
|
|---|
| 34 | thread_2->join();
|
|---|
| 35 |
|
|---|
| 36 | return 0;
|
|---|
| 37 | }
|
|---|