13
13
14
14
int main (int argc, char *argv[])
15
15
{
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 );
17
17
std::string connect_string = " user=postgres password=postgres dbname=test" ;
18
18
19
19
if (argc > 1 )
@@ -23,16 +23,14 @@ int main(int argc, char *argv[])
23
23
24
24
boost::asio::io_service fg_ios;
25
25
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
28
28
boost::thread fg (boost::bind (&boost::asio::io_service::run, &fg_ios));
29
29
boost::thread bg (boost::bind (&boost::asio::io_service::run, &bg_ios));
30
30
31
-
32
31
// precondition CREATE TABLE postgres_asio_sample ( id integer, val text )
33
32
34
-
35
- for (int i = 0 ; i != 500 ; ++i)
33
+ for (int i = 0 ; i != 50 ; ++i)
36
34
{
37
35
auto connection = boost::make_shared<postgres_asio::connection>(fg_ios, bg_ios);
38
36
connection->set_log_id (" xxxx-xxxx-xx" + std::to_string (i));
@@ -41,37 +39,34 @@ int main(int argc, char *argv[])
41
39
if (!ec)
42
40
{
43
41
std::string statement = " insert into postgres_asio_sample (id, val) VALUES\n " ;
44
- size_t items = 10000 ;
42
+ size_t items = 100 ;
45
43
for (int i = 0 ; i != items; ++i)
46
44
{
47
45
const char * ch = i < (items - 1 ) ? " ,\n " : " ;\n " ;
48
46
std::string val = to_string (boost::uuids::random_generator ()());
49
47
statement += " (" + std::to_string (i) + " , '" + val + " ')" + ch;
50
48
}
51
49
52
- std::cerr << connection-> get_log_id ( ) << " inserting!! " << std::endl ;
50
+ BOOST_LOG_TRIVIAL (info ) << " async_insert " ;
53
51
connection->exec (statement, [connection](int ec, boost::shared_ptr<PGresult> res)
54
52
{
55
53
if (ec)
56
54
{
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 ();
58
56
return ;
59
57
}
60
- std::cerr << connection-> get_log_id ( ) << " done!!" << std::endl ;
58
+ BOOST_LOG_TRIVIAL (info ) << " async insert - done!!" ;
61
59
});
62
60
}
63
61
});
64
62
}
65
63
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" ;
75
68
bg.join ();
69
+ BOOST_LOG_TRIVIAL (debug) << " fg join" ;
76
70
fg.join ();
71
+ BOOST_LOG_TRIVIAL (debug) << " done" ;
77
72
}
0 commit comments