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?

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@XavierAriasBotargues: Dispatch queues and blocks are available to C, C++ and Objective-C, compare developer.apple.com/library/mac/#featuredarticles/BlocksGCD/….
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

2 Comments

Have you included sched.h ?
yes, but SCHED_IDLE is not defined, only SCHED_OTHER, SCHED_FIFO and SCHED_RR

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.