diff --git a/src/engine.c b/src/engine.c
index 71ce1f316ffbdbd4afb01a6126b5bbf722e4f433..9677b151b9bae35418cf0c2b430999ae0ddf1402 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -798,13 +798,20 @@ void engine_init ( struct engine *e , struct space *s , float dt , int nr_thread
         if ( pthread_create( &e->runners[k].thread , NULL , &runner_main , &e->runners[k] ) != 0 )
             error( "Failed to create runner thread." );
         #if defined(HAVE_SETAFFINITY)
+        
+            /* Set a reasonable queue ID. */
+            e->runners[k].qid = cpuid[ k ] * nr_queues / nr_threads;
+            
             /* Set the cpu mask to zero | e->id. */
             CPU_ZERO( &cpuset );
-            CPU_SET( cpuid[ e->runners[k].id ] , &cpuset );
+            CPU_SET( cpuid[ k ] , &cpuset );
 
             /* Apply this mask to the runner's pthread. */
             if ( pthread_setaffinity_np( e->runners[k].thread , sizeof(cpu_set_t) , &cpuset ) != 0 )
                 error( "Failed to set thread affinity." );
+                
+        #else
+            e->runners[k].qid = k % nr_queues;
         #endif
         }
         
diff --git a/src/runner.c b/src/runner.c
index 2ba35a5e865bdfca5f1b0f332bbb5e9195dd6754..000e2abcf75b239ec84399c4bb2b731b6f53201c 100644
--- a/src/runner.c
+++ b/src/runner.c
@@ -768,7 +768,7 @@ void *runner_main ( void *data ) {
     struct runner *r = (struct runner *)data;
     struct engine *e = r->e;
     struct scheduler *sched = &e->sched;
-    int threadID = r->id;
+    int qid = r->qid;
     struct task *t;
     struct cell *ci, *cj;
     
@@ -783,7 +783,7 @@ void *runner_main ( void *data ) {
         
             /* Get a task, how and from where depends on the policy. */
             TIMER_TIC
-            t = scheduler_gettask( sched , threadID );
+            t = scheduler_gettask( sched , qid );
             TIMER_TOC(timer_getpair);
             
             /* Did I get anything? */
diff --git a/src/runner.h b/src/runner.h
index 5ee1ee3a95c36b2e70e6c656cd2a95567c565f84..c6d2e656ab1d1a40dc790be29c096341c00ef859 100644
--- a/src/runner.h
+++ b/src/runner.h
@@ -62,6 +62,9 @@ struct runner {
     /* The thread which it is running. */
     pthread_t thread;
     
+    /* The queue to use to get tasks. */
+    int qid;
+
     /* The underlying runner. */
     struct engine *e;