日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux非阻塞等待线程,linux – 即使异步I / O操作挂起,只有线程处理io_service正在等待...

發布時間:2024/9/27 linux 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux非阻塞等待线程,linux – 即使异步I / O操作挂起,只有线程处理io_service正在等待... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這是一個錯誤.我已經能夠通過在task_io_service :: do_poll_one的非關鍵部分添加延遲來復制它.以下是

booost/asio/detail/impl/task_io_service.ipp中修改后的task_io_service :: do_poll_one()的片段.添加的唯一行是sleep.

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;

}

...

我的測試驅動程序非常基礎:

>通過計時器進行異步工作循環,打印“.”每3秒鐘一次.

>生成一個將輪詢io_service的線程.

>延遲允許新線程時間輪詢io_service,并且當poll線程在task_io_service :: do_poll_one()中休眠時,主調用io_service :: run().

測試代碼:

#include

#include

#include

#include

#include

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();

}

調試:

[twsansbury@localhost 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()已打印“.”曾經(當它被武裝起來時). poll線程以非阻塞方式為反應器提供服務,并且在op_queue_為空時睡眠3秒(當task_cleanup c退出范圍時,task_operation_將被添加回op_queue_).當op_queue_為空時,主線程調用io_service :: run(),看到op_queue_為空,并使自己成為first_idle_thread_,它在wakeup_event上等待. poll線程完成休眠,并返回0,主線程等待wakeup_event.

等待10秒后,arm_timer()有足夠的時間準備就緒,我打斷調試器:

Program received signal SIGINT,Interrupt.

0x00919402 in __kernel_vsyscall ()

(gdb) bt

#0 0x00919402 in __kernel_vsyscall ()

#1 0x0081bbc5 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib/libpthread.so.0

#2 0x00763b3d in pthread_cond_wait@@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 ()

并排時間表如下:

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() |

盡我所知,修補沒有副作用:

if (o == &task_operation_)

return 0;

至:

if (o == &task_operation_)

{

if (!one_thread_)

wake_one_thread_and_unlock(lock);

return 0;

}

無論如何,我已經提交了bug and fix.考慮留意官方回復的機票.

總結

以上是生活随笔為你收集整理的linux非阻塞等待线程,linux – 即使异步I / O操作挂起,只有线程处理io_service正在等待...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。