简体   繁体   English

带pthread的后台线程

[英]Background thread with pthreads

i'm using pthreads to create a background thread to load and do some tasks in background, but it lags the application a little bit as it's work is intense sometimes. 我正在使用pthreads创建后台线程以在后台加载并执行一些任务,但是由于有时工作量很大,因此它会使应用程序滞后一些。

Is there any way i can set a lower priority or a different scheduling (or combination of both) so the main thread has more CPU time to run? 有什么办法可以设置较低的优先级或不同的调度(或两者结合),以便主线程有更多的CPU时间运行?

Use pthread_attr_setschedparam http://linux.die.net/man/3/pthread_attr_setschedparam 使用pthread_attr_setschedparam http://linux.die.net/man/3/pthread_attr_setschedparam

Use SCHED_IDLE scheduling policy to lower the priority of thread 使用SCHED_IDLE调度策略降低线程的优先级

Hope below SO links may help. 希望下面的SO链接可能会有所帮助。

How to increase thread priority in pthreads? 如何在pthreads中增加线程优先级?

Equivalent of SetThreadPriority on Linux (pthreads) 在Linux(pthreads)上等效于SetThreadPriority

Unable to set Pthread Priority 无法设置Pthread优先级

This does not verbatim answer your question, but in the Concurrency Programming Guide Apple recommends to move away from threads and use "Dispatch Queues" or "Operation Queues" for asynchronous operations on OS X and iOS, for example 这不能完全回答您的问题,但是Apple建议在《 并发编程指南》中移开线程,并在OS X和iOS上对异步操作使用“调度队列”或“操作队列”。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    // ... background task
});

and DISPATCH_QUEUE_PRIORITY_LOW is documented as 并且DISPATCH_QUEUE_PRIORITY_LOW被记录为

Items dispatched to the queue run at low priority; 调度到队列的项目的优先级较低; the queue is scheduled for execution after all default priority and high priority queues have been scheduled. 在已调度所有默认优先级和高优先级队列之后,将调度该队列执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM