latency for posting to io_service

Hi,

I have a boost::asio::io_service running in a separate thread, and I want to run some async functions in the background without stalling the main thread. However I found that the first io.post takes quite a long time, compare with the subsequent ones. I wonder if there are any remedy for this?


1
2
3
4
5
6
7
8
9
10
	for (int i = 1; i < 10; i++) {
		start = boost::chrono::high_resolution_clock::now();
		io.post(&CalcPi);
		nano_sec = boost::chrono::high_resolution_clock::now() - start;
		cout << "[" << this_thread::get_id() << "] " << i << "\tCalcPi takes " << nano_sec.count()/1000. << " us\n";
	}
	start = boost::chrono::high_resolution_clock::now();
	CalcPi();
	nano_sec = boost::chrono::high_resolution_clock::now() - start;
	cout << "actual CalcPi takes " << nano_sec.count()/1000000000. << " s" << endl;


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[140171152087136] 1	CalcPi takes 53.254 us
[140171152087136] 2	CalcPi takes 1.111 us
[140171152087136] 3	CalcPi takes 0.365 us
[140171152087136] 4	CalcPi takes 0.375 us
[140171152087136] 5	CalcPi takes 0.307 us
[140171152087136] 6	CalcPi takes 0.31 us
[140171152087136] 7	CalcPi takes 0.306 us
[140171152087136] 8	CalcPi takes 0.369 us
[140171152087136] 9	CalcPi takes 0.305 us
Pi = 3.14159
actual CalcPi takes 1.60752 s
Pi = 3.14159
Pi = 3.14159
Pi = 3.14159
Pi = 3.14159
Pi = 3.14159
Pi = 3.14159
Pi = 3.14159


Now if I wait 2 seconds (which is more than CalcPi takes) in between each post, the post latency is similar to the first one (slow)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[139637407103072] 1	CalcPi takes 23.17 us
Pi = 3.14159
[139637407103072] 2	CalcPi takes 13.101 us
Pi = 3.14159
[139637407103072] 3	CalcPi takes 12.206 us
Pi = 3.14159
[139637407103072] 4	CalcPi takes 28.965 us
Pi = 3.14159
[139637407103072] 5	CalcPi takes 31.213 us
Pi = 3.14159
[139637407103072] 6	CalcPi takes 14.479 us
Pi = 3.14159
[139637407103072] 7	CalcPi takes 12.939 us
Pi = 3.14159
[139637407103072] 8	CalcPi takes 14.673 us
Pi = 3.14159
[139637407103072] 9	CalcPi takes 13.821 us
Pi = 3.14159
Pi = 3.14159
actual CalcPi takes 1.48618 s


So it seems that io.post is slow when there is nothing there to be run. Are there any ways to keep io.post to return quickly? I don't mind small delays in running the actual call itself.
Last edited on
> However I found that the first io.post takes quite a long time, compare with the subsequent ones.

Try posting a dummy handler first: io.post( [] { std::cout << "this dummy handler does nothing\n" ; }

This should bring the relevant code into the processor cache, and ensure that the internal infrastructure for asynchronous execution of handlers is initialised.
This is just a guess, but it might also be that the first call to io.post() has to setup a communications link. For example if it's setting up a TCP connection then that might explain the delay.
Topic archived. No new replies allowed.