tags:

views:

754

answers:

1

My application creates a helper pthread that I need to have run at a higher priority than the main thread.

I tried to set the priority of the created thread like this:

struct sched_param param;
pthread_attr_t tattr;
pthread_attr_init(&tattr);
pthread_attr_getschedparam(&tattr, &param);
param.sched_priority = sched_get_priority_max(SCHED_RR);
pthread_attr_setschedparam(&tattr, &param);
pthread_create(&helper_thread, &tattr, helper_main, NULL);

But the pthread_attr_setschedparam call is returning EINVAL.

I'm not sure what I'm doing wrong here. Is this even the right general approach?

I don't really care how it gets done I just need the new thread to have a higher priority than the original one.

+2  A: 

Setting a priority under the default scheduling policy (SCHED_OTHER) isn't valid. You need to switch to the SCHED_RR policy if you want to use a SCHED_RR priority:

pthread_attr_setschedpolicy(&tattr, SCHED_RR);

You also shouldn't be setting it to the maximum priority - if you just want it higher priority than the main thread, then a priority of 1 will be sufficient. (You may find that setting the maximum priority fails with EPERM if you're not root, anyway).

caf
sweet, that works thanks
tolomea