Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run different threads on different cores? [duplicate]

Possible Duplicate:
how to set CPU affinity of a particular pthread?

I am writing a c++ program, using g++ compiler in Ubuntu. I have 4 threads in my program and 4 cores on my CPU. I want to be sure that each thread will be run on a different core. I am rarely familiar with pthread.

like image 875
Saeed Avatar asked Sep 25 '11 10:09

Saeed


2 Answers

See sched_setaffinity function: http://manpages.courier-mta.org/htmlman2/sched_setaffinity.2.html

like image 71
Alex F Avatar answered Oct 20 '22 08:10

Alex F


Don't do this. Let the system schedule the threads. If you affinitise the threads to distinct cores you just handicap the scheduler. When your app is the only one consuming CPU, the scheduler will naturally schedule each thread on a separate core.

like image 26
David Heffernan Avatar answered Oct 20 '22 06:10

David Heffernan