- 论坛徽章:
- 0
|
本帖最后由 tweyseo 于 2011-11-30 23:12 编辑
回复 2# nth2say
恩 我看了下代码 可是还有2个疑问:
第一按照这样的话,我下面的代码应该是dispatch那边的话 应该是先打印数字在打印"dispatch",可是实际上是反过来的.- boost::asio::io_service io_ser;
- boost::asio::strand schedule(io_ser);
- boost::mutex io_mutex;
- void counter(int id)
- {
- boost::mutex::scoped_lock lock(io_mutex);
- std::cout << "I'm in thread[" << id << "]" << std::endl;
- }
- void NewThreadPost(int id)
- {
- schedule.post(schedule.wrap(boost::bind(&counter, id)));
- boost::mutex::scoped_lock lock(io_mutex);
- std::cout << "post." << std::endl;
- }
- void NewThreadDispatch(int id)
- {
- schedule.dispatch(schedule.wrap(boost::bind(&counter, id)));
- boost::mutex::scoped_lock lock(io_mutex);
- std::cout << "dispatch." << std::endl;
- }
- int main()
- {
- boost::thread_group tg;
- //tg.create_thread(boost::bind(&NewThreadDispatch, 1));
-
- tg.create_thread(boost::bind(&NewThreadPost, 2));
- NewThreadDispatch(1);
-
- io_ser.run();
- //tg.join_all();
- return 0;
- }
复制代码 第二,我看dispatch的代码还有一段- // If we are already in the strand then the handler can run immediately.
- if (call_stack<strand_impl>::contains(impl))
- {
- boost::asio::detail::fenced_block b;
- boost_asio_handler_invoke_helpers::invoke(handler, handler);
- return;
- }
复制代码 这个又是在说明一种和post不同的地方,但是具体是什么呢? |
|