0

I am writing a program that will create two threads, one of them has to have a high pripoity and the other is default. I am using pthread_create() to create the thread and would like to initiate the thread priority from the same command. The way I am doing that is as follow:

pthread_create(&threads[lastThreadIndex], NULL, &solution, (void *)(&threadParam));

where, threads: is an array of type pthread_t that has all my threads in. lastThreadIndex: is a counter solution: is my function threadParam: is a struct that has all variables needed by the solution function.

I have read many articles and most of them suggest to replace NULL with the priority level; However, I never found the level key word or the exact way of doing it.

Please help...

Thanks

1 Answer 1

3

In POSIX, that second parameter is the pthread attribute, and NULL just means to use the default.

However, you can create your own attribute and set its properties, including driving up the priority with something like:

#include <pthread.h> #include <sched.h> int rc; pthread_attr_t attr; struct sched_param param; rc = pthread_attr_init (&attr); rc = pthread_attr_getschedparam (&attr, &param); (param.sched_priority)++; rc = pthread_attr_setschedparam (&attr, &param); rc = pthread_create (&threads[lastThreadIndex], &attr, &solution, (void *)(&threadParam)); // Should really be checking rc for errors. 

Details on POSIX threading, including scheduling, can be found starting at this page.

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

4 Comments

Can you please provide a good reference to learn how to do this. The problem I am facing is that I have a program that deals with a matrix with 3200 dimension. I broke down the matrix into 4 sub matrices and sends each submatrix on a thread to be manipulated. The execution time I get on windows machine is 6521 microseconds (which is almost the same as the sequential code execution time 6600msec) ; However, when I run the same program on a linux machine it runs in 860 microseconds (the sequential code execution time on the linux machine is 6300msec).
@Anas, if you're using pthreads on windows, it's probably pthreads_win32. If so, it's (how can I say this tactfully?) ... less optimised than the Linux kernel, based on my experience :-) You may be better off using Windows native threads.
Do you mean open MP on windows ? or there is another way ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.