Bu bir hatadır. task_io_service::do_poll_one
'un kritik olmayan bölümüne bir gecikme ekleyerek bunu kopyalayabildim. İşte, booost/asio/detail/impl/task_io_service.ipp
'da değiştirilmiş task_io_service::do_poll_one()
'un bir pasajı. Eklenen tek satır uykudır.
std::size_t task_io_service::do_poll_one(mutex::scoped_lock& lock,
task_io_service::thread_info& this_thread,
const boost::system::error_code& ec)
{
if (stopped_)
return 0;
operation* o = op_queue_.front();
if (o == &task_operation_)
{
op_queue_.pop();
lock.unlock();
{
task_cleanup c = { this, &lock, &this_thread };
(void)c;
// Run the task. May throw an exception. Only block if the operation
// queue is empty and we're not polling, otherwise we want to return
// as soon as possible.
task_->run(false, this_thread.private_op_queue);
boost::this_thread::sleep_for(boost::chrono::seconds(3));
}
o = op_queue_.front();
if (o == &task_operation_)
return 0;
}
...
Testim sürücüsü oldukça basit: ""
- yazdırır bir zamanlayıcı aracılığıyla Zaman uyumsuz çalışma döngüsü her 3 saniyede.
io_service
'u yoklayacak tek bir iş parçacığı oluşturur.
- Yeni iş parçacığı saatinin
io_service
yoklanmasını sağlamak için geciktirmek ve yoklama dizesi task_io_service::do_poll_one()
'da uyurken ana aramayı io_service::run()
olsun.
Testi kodu:
#include <iostream>
#include <boost/asio/io_service.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/chrono.hpp>
#include <boost/thread.hpp>
boost::asio::io_service io_service;
boost::asio::steady_timer timer(io_service);
void arm_timer()
{
std::cout << ".";
std::cout.flush();
timer.expires_from_now(boost::chrono::seconds(3));
timer.async_wait(boost::bind(&arm_timer));
}
int main()
{
// Add asynchronous work loop.
arm_timer();
// Spawn poll thread.
boost::thread poll_thread(
boost::bind(&boost::asio::io_service::poll, boost::ref(io_service)));
// Give time for poll thread service reactor.
boost::this_thread::sleep_for(boost::chrono::seconds(1));
io_service.run();
}
Ve ayıklama: Bu noktada
[[email protected] bug]$ gdb a.out
...
(gdb) r
Starting program: /home/twsansbury/dev/bug/a.out
[Thread debugging using libthread_db enabled]
.[New Thread 0xb7feeb90 (LWP 31892)]
[Thread 0xb7feeb90 (LWP 31892) exited]
, arm_timer()
bastırdı "" bir kez (ne zaman silahlı olduğunda). Anket parçacığı reaktöre bloke olmayan bir şekilde hizmet etti ve op_queue_
boş iken 3 saniye uyudu (task_operation_
, task_cleanup c
kapsam dışına çıktığında op_queue_
'a eklenir). op_queue_
boşken, ana iş parçacığı io_service::run()
çağırır, op_queue_
boş olduğunu görür ve , wakeup_event
üzerinde bekler. Anket dizisi uykuyu bitirir ve 0
değerini döndürür ve ana iş parçacığı wakeup_event
tarihinde bekler.
Program received signal SIGINT, Interrupt.
0x00919402 in __kernel_vsyscall()
(gdb) bt
#0 0x00919402 in __kernel_vsyscall()
#1 0x0081bbc5 in [email protected]@GLIBC_2.3.2() from /lib/libpthread.so.0
#2 0x00763b3d in [email protected]@GLIBC_2.3.2() from /lib/libc.so.6
#3 0x08059dc2 in void boost::asio::detail::posix_event::wait >(boost::asio::detail::scoped_lock&)()
#4 0x0805a009 in boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&)()
#5 0x0805a11c in boost::asio::detail::task_io_service::run(boost::system::error_code&)()
#6 0x0805a1e2 in boost::asio::io_service::run()()
#7 0x0804db78 in main()
yan-yana zaman çizelgesi aşağıdaki gibidir:
10 ~ saniye, arm_timer()
hazır olmak için bol zaman bekledikten sonra hata ayıklayıcı kesme
poll thread | main thread
---------------------------------------+---------------------------------------
lock() |
do_poll_one() |
|-- pop task_operation_ from |
| queue_op_ |
|-- unlock() | lock()
|-- create task_cleanup | do_run_one()
|-- service reactor (non-block) | `-- queue_op_ is empty
|-- ~task_cleanup() | |-- set thread as idle
| |-- lock() | `-- unlock()
| `-- queue_op_.push( |
| task_operation_) |
`-- task_operation_ is |
queue_op_.front() |
`-- return 0 | // still waiting on wakeup_event
unlock() |
söyler misiniz
olarak iyi, yama yaparak hiçbir yan etkileri vardır:
if (o == &task_operation_)
return 0;
için: Ne olursa olsun
if (o == &task_operation_)
{
if (!one_thread_)
wake_one_thread_and_unlock(lock);
return 0;
}
, ben bug and fix sundular. Resmi bir cevap için bilete göz atmayı düşünün.
run_one() 'dan dönüş kodu nedir? –
, normal 1 döndürürse, yalnızca 0 döndürdüğünde, io_service sıfırlanmalıdır. Yanlış bir şey yaptığın gibi gelmiyor, bir sscce gönderebilir misin? –
@DavidSchwartz pthread_cond_timedwait çağrısının asio'dan olduğundan emin misiniz? Kodda bunu görmekte zorluk çekiyorum. – janm