How to bind a thread to specific thread in multithread application?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to bind a thread to specific thread in multithread application?
Dear all;
I am going to use "pthread_setaffinity_np" to bind a thread to a specific core. My application has two threads. I have used mutex to assign a specific id to each thread and then bind that thread to a core different from another core.
but it seems that the os assigns both thread to one core.
What should I do to bind each thread to a specific core?
id = 0;
...
cpu_set_t myset;
pthread_mutex_lock(&mutex);
MyNum = id;
CPU_ZERO(&myset);
CPU_SET(MyNum+1, &myset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t),&myset);
id ++;
pthread_mutex_unlock(&mutex);
If you talk about core I think you have a multicore CPU. So "pthread_setaffinity_np" function can not do anything useful for you, because you only have one CPU (with more cores), if I did read it right at http://www.unix.com/man-page/All/3/P...ETAFFINITY_NP/.
I think there is a discussion at GCC, glibc about that problem, too.
I also think it is easier to read out the core's register (SYSCFG (a MSR) I guess) and exit the pthread on a control mechanism.
Maby I can help you to find a way out if you explain, why you need to have a thread running on a special core.
Thank you very much for your help.
I have run a multithread application on ubuntu. While I watch the system monitor, just one cpu is completely used, while the other one has almost zero cpu usage.
This property is against the goal of multithread applications. I think if the os distributes each thread on a core, it will gain better performance.
In System monitor, it shows two cpus for my system. What is the difference between core and cpu then?
O.K., I don't know ubuntu verry much but the thread optimization is done by the kernel.
You need a kernel with multicore support enabled ( Multi-core scheduler support (SCHED_MC) ).
Then on a multicore CPU ( 4 or more cores ) every time a thread/process is created the thread/process is set to the next core.
Duo core machines are handled different but I can not say if they are handled the way like in your case - wouldn't make much sense, I have to agree, - but they are limited.
There are multiprocessor systems where the system resouces have to (can) be set up for each processor.
A IA-Multicore processor it verry limited about that (pins, external connections).
Else there are no differences (compute time).
On the other hand I don't know what that monitor app. is representing (does measuring).
id = 0;
...
cpu_set_t myset;
pthread_mutex_lock(&mutex);
MyNum = id;
CPU_ZERO(&myset);
CPU_SET(MyNum+1, &myset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t),&myset);
id ++;
pthread_mutex_unlock(&mutex);
...
Hey,
I'm not sure if that solves your problems, but the CPU number usually starts at 0 and NOT 1 (you could easily verify this by using top(1) or the sysFS). So, maybe you could try CPU_SET(MyNum, &myset) instead?
Secondly, please check and verify the return value of 'pthread_setaffinity_np'. It should tell you if you specified an incorrect CPU mask.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.