LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-25-2010, 04:22 AM   #1
h.aliee
LQ Newbie
 
Registered: Sep 2010
Posts: 27

Rep: Reputation: 0
Question 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);

...

Thanks in advance for your help.
 
Old 12-27-2010, 06:59 PM   #2
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
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.
 
Old 12-28-2010, 01:04 AM   #3
h.aliee
LQ Newbie
 
Registered: Sep 2010
Posts: 27

Original Poster
Rep: Reputation: 0
Hi Bastl,

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?
 
Old 12-28-2010, 04:22 PM   #4
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
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).
 
Old 12-30-2010, 03:11 PM   #5
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by h.aliee View Post
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.

I hope that helps,
Andi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
Another Bind problem thread riotpunk Linux - Server 4 10-29-2007 02:46 PM
FC4 (2.6.13) SMP multithread coredump missing second thread register state anon128 Linux - Kernel 0 08-02-2006 10:31 AM
Yet another recommend me a distro thread (specific) kag Linux - General 9 08-13-2002 11:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration