取消挂在epoll_wait上的线程(Cancelling thread that is stuck on epoll_wait)
我正在用C ++和pthreads进行一些事件处理。 我有一个从我定义的事件队列读取的主线程和一个填充事件队列的工作线程。 队列当然是线程安全的。
工作线程有一个文件描述符列表,并创建一个epoll系统调用来获取这些文件描述符上的事件。 它使用epoll_wait等待fd上的事件。
现在的问题。 假设我想彻底终止我的应用程序,我该如何正确取消工作线程?
epoll_wait
不是pthread(7)的取消点之一,所以它不能在pthread_cancel
正确反应。工作线程main()看起来像这样
while(m_WorkerRunning) { epoll_wait(m_EpollDescriptor, events, MAXEVENTS, -1); //handle events and insert to queue }
当线程启动时,
m_WorkerRunning
设置为true
,看起来我可以通过主线程的设置m_WorkerRunning
将其设置为false 来中断线程。 问题是epoll_wait
理论上可以永远等待。我的其他解决方案是:不是等待永久(-1),我可以等待例如X时隙,然后正确处理无事件的情况,如果
m_WorkerRunning == false
则退出循环并干净地终止工作线程。 主线程然后将m_WorkerRunning
设置为false,并休眠X.但是我不确定这种epoll_wait的性能,也不确定什么是正确的X? 500ms的? 1秒? 10秒?我想听听一些有经验的建议!
更多相关信息:我正在等待事件的fd是
/dev/input
中的设备,因此技术上我正在做某种输入子系统。 目标操作系统是ARM架构上的Linux(最新内核)。谢谢!
I'm doing some event handling with C++ and pthreads. I have a main thread that reads from event queue I defined, and a worker thread that fills the event queue. The queue is of course thread safe.
The worker thread have a list of file descriptors and create an epoll system call to get events on those file descriptors. It uses epoll_wait to wait for events on the fd's.
Now the problem. Assuming I want to terminate my application cleanly, how can I cancel the worker thread properly?
epoll_wait
is not one of the cancellation points of pthread(7) so it cannot react properly onpthread_cancel
.The worker thread main() looks like this
while(m_WorkerRunning) { epoll_wait(m_EpollDescriptor, events, MAXEVENTS, -1); //handle events and insert to queue }
The
m_WorkerRunning
is set totrue
when the thread starts and it looks like I can interrupt the thread by settingsm_WorkerRunning
to false from the main thread. The problem is thatepoll_wait
theoretically can wait forever.Other solution I though about is: instead of waiting forever (-1) I can wait for example X time slots, then handle properly no-events case and if
m_WorkerRunning == false
then exit the loop and terminate the worker thread cleanly. The main thread then setsm_WorkerRunning
to false, and sleeps X. However I'm not sure about the performance of such epoll_wait and also not sure what would be the correct X? 500ms? 1s? 10s?I'd like to hear some experienced advises!
More relevant information: the fd's I'm waiting events on, are devices in
/dev/input
so technically I'm doing some sort of input subsystem. The targeted OS is Linux (latest kernel) on ARM architecture.Thanks!
原文:https://stackoverflow.com/questions/18614168
最满意答案
Struts操作没有参数并返回String。
你想要做的是在你的动作类中有一个带有getter / setter的String boo。 然后它将在您的方法中可用。 如果您有默认的拦截器堆栈(请参阅http://struts.apache.org/2.2.3/docs/interceptors.html ),它将根据您的struts.xml配置自动填充“boo”。
Struts actions do not have parameters and return a String.
What you want to do is have a String boo in your action class with getter/setters. It will then be available in your method. And if you have the default interceptor stack (see http://struts.apache.org/2.2.3/docs/interceptors.html) it will automatically be populated with "boo" according to your struts.xml configuration.
相关问答
更多-
请首先确保您的操作扩展了DispatchAction。 您可能不应该重写该类中的execute方法,因为该方法负责提取请求参数并调用相应的方法。 如果你重写执行这个逻辑将不再执行。 Please first make sure that your action extends DispatchAction. You probably should not override the execute method in that class since that method is responsible fo ...
-
找到了解决方案,我为html添加了一个隐藏字段,其中值是方法的名称
相关文章
更多- 引入thread后socket接受不了报文了
- 线程报错 thread
- 关于Thread类中的start()方法和run()方法
- 一步一步掌握java的线程机制(二)----Thread的生命周期
- 每日英语:Millions Stuck In Dark, Cold
- org.apache.zookeeper.server.NIOServerCnxn - Thread Thread[main,5,main] died java.lang.NoSuchFieldError: AuthFailed
- 为什么执行JAVA程序时,会出现Exception in thread"main" java.lan
- 一步一步掌握线程机制(五)---等待与通知机制
- linux内核bug问题排查过程详细报告
- 关于线程
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)