Skip to content

Commit daacd79

Browse files
committed
fixed running-forever
1 parent c08f25d commit daacd79

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

samples/insert_sample/insert_sample.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
int main(int argc, char *argv[])
1515
{
16-
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info);
16+
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::debug);
1717
std::string connect_string = "user=postgres password=postgres dbname=test";
1818

1919
if (argc > 1)
@@ -23,16 +23,14 @@ int main(int argc, char *argv[])
2323

2424
boost::asio::io_service fg_ios;
2525
boost::asio::io_service bg_ios;
26-
std::auto_ptr<boost::asio::io_service::work> work2(new boost::asio::io_service::work(fg_ios));
27-
std::auto_ptr<boost::asio::io_service::work> work1(new boost::asio::io_service::work(bg_ios));
26+
std::auto_ptr<boost::asio::io_service::work> fg_work(new boost::asio::io_service::work(fg_ios)); // this keeps the fg_ios alive
27+
std::auto_ptr<boost::asio::io_service::work> bg_work(new boost::asio::io_service::work(bg_ios)); // this keeps the bg_ios alive
2828
boost::thread fg(boost::bind(&boost::asio::io_service::run, &fg_ios));
2929
boost::thread bg(boost::bind(&boost::asio::io_service::run, &bg_ios));
3030

31-
3231
// precondition CREATE TABLE postgres_asio_sample ( id integer, val text )
3332

34-
35-
for (int i = 0; i != 500; ++i)
33+
for (int i = 0; i != 50; ++i)
3634
{
3735
auto connection = boost::make_shared<postgres_asio::connection>(fg_ios, bg_ios);
3836
connection->set_log_id("xxxx-xxxx-xx" + std::to_string(i));
@@ -41,37 +39,34 @@ int main(int argc, char *argv[])
4139
if (!ec)
4240
{
4341
std::string statement = "insert into postgres_asio_sample (id, val) VALUES\n";
44-
size_t items = 10000;
42+
size_t items = 100;
4543
for (int i = 0; i != items; ++i)
4644
{
4745
const char* ch = i < (items - 1) ? ",\n" : ";\n";
4846
std::string val = to_string(boost::uuids::random_generator()());
4947
statement += " (" + std::to_string(i) + ", '" + val + "')" + ch;
5048
}
5149

52-
std::cerr << connection->get_log_id() << " inserting!!" << std::endl;
50+
BOOST_LOG_TRIVIAL(info) << "async_insert";
5351
connection->exec(statement, [connection](int ec, boost::shared_ptr<PGresult> res)
5452
{
5553
if (ec)
5654
{
57-
std::cerr << connection->get_log_id() << " insert failed ec:" << ec << " last_error:" << connection->last_error() << std::endl;
55+
BOOST_LOG_TRIVIAL(error) << " insert failed ec:" << ec << " last_error:" << connection->last_error();
5856
return;
5957
}
60-
std::cerr << connection->get_log_id() << " done!!" << std::endl;
58+
BOOST_LOG_TRIVIAL(info) << "async insert - done!!";
6159
});
6260
}
6361
});
6462
}
6563

66-
while (true)
67-
{
68-
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
69-
}
70-
71-
work1.reset();
72-
work2.reset();
73-
//bg_ios.stop();
74-
//fg_ios.stop();
64+
BOOST_LOG_TRIVIAL(debug) << "work reset";
65+
fg_work.reset();
66+
bg_work.reset();
67+
BOOST_LOG_TRIVIAL(debug) << "bg join";
7568
bg.join();
69+
BOOST_LOG_TRIVIAL(debug) << "fg join";
7670
fg.join();
71+
BOOST_LOG_TRIVIAL(debug) << "done";
7772
}

0 commit comments

Comments
 (0)