I wanted to set priority of a custom threadpool developed. so I found to set the priority need to use pthred's pthread_setschedparam
method. Since pthread_t
and native_handle_type
is different I did something like this -
void SetPriority(int id, int priority, int policy) {
bool ret = true;
sched_param sch_params;
sch_params.sched_priority = 20;
pthread_t p;
p.p = threads_[id].native_handle();
if (pthread_setschedparam(p, SCHED_FIFO, &sch_params)) {
std::cerr << "Failed to set Thread scheduling : " << std::strerror(errno) << std::endl;
}
}
I am getting Access violation for pthreadVC2.dll
.
To test if I am with the right windows version's dll I made did some test like this -
int main(){
pthread_t thread1, thread2;
int thr = 1;
int thr2 = 2;
// start the threads
pthread_create(&thread1, NULL, *threadfn, (void *)thr);
pthread_create(&thread2, NULL, *threadfn, (void *)thr2);
sched_param sch_params;
sch_params.sched_priority = 20;
if (pthread_setschedparam(thread1, SCHED_FIFO, &sch_params)) {
std::cerr << "Failed to set Thread scheduling : " << std::strerror(errno) << std::endl;
}
// wait for threads to finish
int po1, po2;
sched_param sched_param1, sched_param2;
pthread_getschedparam(thread1,&po1,&sched_param1);
pthread_getschedparam(thread2, &po2, &sched_param2);
cout << "thread 1:policy - " << po1 << " priority - " << sched_param1.sched_priority << endl;
cout << "thread 2:policy - " << po2 << " priority - " << sched_param2.sched_priority << endl;
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
I found error no is 1 (priority not set). And both thread having policy 0 & priority 0
What is the proper way to set priority of a pthread and how can I use this functionality from std::thread ?
Edit: Windows environment setup: Using Visual Studio 2015 with windows 10. Downloaded pthread (pthreads-w32-2-9-1-release.zip) from ftp://sourceware.org/pub/pthreads-win32/. project setting- included Additional Include Directories Linker > General and set the location of the pthread library Additional dependecies : pthreadVC2.lib and pthreadVCE2.lib and copied pthreadVC2.dll in the project directory.
native_handle()
is likely intended to be used with the (native)SetThreadPriority
. I have no experience with the pthread package on Windows. – Bo Persson