2

I am trying to execute my program in threads, I use pthread_create(), but it runs the threads immediately. I would like to allow the user to change thread priorities before running. How it is possible to resolve?

for(int i = 0; i < threads; i++)
{
   pthread_create(data->threads+i,NULL,SelectionSort,data);
   sleep(1);
   print(data->array);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • any way you can set the priority using the attr argument of pthread_create – pm100 Dec 13 '17 at 17:23
  • @pm100, I know how to set priority, the problem is how don't run the created threads immediately but allow the user to run it or change priorities before running – mark blacksmith Dec 13 '17 at 17:28
  • 1
    Look by your image of code (BAD) you are using C++, please don't tag C and C++ unless there is a reason. You are coding in C++, tag only C++, thanks. – Stargateur Dec 13 '17 at 17:40
  • You need to attempt a solution before asking. Asking for user input before creating the thread. Try passing a different routine to pthread_create() that takes in user input. Look into using process control structures to sync the start and stops. – Jeffery Thomas Dec 13 '17 at 19:32
  • Others have already explained how to use attributes to set the thread priority during thread creation. In a broader scope, [pthreads does not have an option to create a thread in an initially-suspended state](https://stackoverflow.com/questions/7953917/), so whatever you want to do to configure the thread (priority, cpu affinity, etc) must be done during the thread's creation, if it can't be applied after the thread is already running. – Remy Lebeau Dec 13 '17 at 21:14

2 Answers2

3

Set the priority as you create the thread.

Replace

errno = pthread_create(..., NULL, ...);
if (errno) { ... }

with

pthread_attr_t attr;
errno = pthread_attr_init(&attr);
if (errno) { ... }

{
    struct sched_param sp;
    errno = pthread_attr_getschedparam(&attr, &sp);
    if (errno) { ... }

    sp.sched_priority = ...;

    errno = pthread_attr_setschedparam(&attr, &sp);
    if (errno) { ... }
}    

/* So our scheduling priority gets used. */
errno = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
if (errno) { ... }

errno = pthread_create(..., &attr, ...);
if (errno) { ... }

errno = pthread_attr_destroy(&attr);
if (errno) { ... }
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • I am trying to do like you but failed(it doesnt change priority ) where could be mistake https://pasteboard.co/GY3urhX.png – mark blacksmith Dec 13 '17 at 18:38
  • What did we tell you about images?!? And that doesn't look anything like what I posted?!? – ikegami Dec 13 '17 at 18:50
  • why is it bad that I give a link to the photo ? Ok how to rearrange the code I posted ? – mark blacksmith Dec 13 '17 at 19:04
  • Re "*why is it bad that I give a link to the photo ?*", It's bad that you didn't provide the contents of the image as text. You can *also* provide as an image if you want. – ikegami Dec 13 '17 at 19:28
  • Re "*how to rearrange the code I posted ?*", The code you should use is in my Answer. – ikegami Dec 13 '17 at 19:29
  • @markblacksmith: "*why is it bad that I give a link to the photo ?*" - posting images of code is wrong, because people can't copy code from an image. Post the code as actual text, and then people can copy/paste it into their code editors and compile it. Also, links can break over time, especially links to sites that are not owned by StackOverflow (which has its own image hosting provider, BTW, so don't use an external image hosting service). – Remy Lebeau Dec 13 '17 at 21:06
1

For pthreads the priority isn't set after thread creation but rather by passing suitable attributes upon thread creation: the thread attributes go where you have specified NULL in your pthread_create() call. If you want to delay thread creation until the user has given you a priority you can create a function object expecting the priority and upon call of that function object you'd kick off the thread. Of course, you'll still need to keep track of the thus created object (possibly using a std::future<...>-like object) to later join that thread.

Note that providing an answer shouldn't be construed as endorsing thread priorities: as far as I can tell, playing with thread priorities are ill-advised.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380