2

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.

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?

2 Answers2

2

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

Use SCHED_IDLE scheduling policy to lower the priority of thread

Hope below SO links may help.

How to increase thread priority in pthreads?

Equivalent of SetThreadPriority on Linux (pthreads)

Unable to set Pthread Priority

Community
  • 1
  • 1
bjskishore123
  • 6,144
  • 9
  • 44
  • 66
2

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

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

and DISPATCH_QUEUE_PRIORITY_LOW is documented as

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • @XavierAriasBotargues: Dispatch queues and blocks are available to C, C++ and Objective-C, compare http://developer.apple.com/library/mac/#featuredarticles/BlocksGCD/_index.html. – Martin R May 20 '13 at 22:56