回答

1

不知道這是有益的,但這裏是一些代碼,將獲得和使用並行線程API設置線程的親和力:

// nail down this thread so it will only run on a specific CPU 
cpu_set_t tCPUSet; 
CPU_ZERO(&tCPUSet); 
CPU_SET(ENCODE_LEFT_THREAD_CPU, &tCPUSet); 
nReturnCode = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &tCPUSet); 
if (nReturnCode == -1) { 
    pLogFile->Write(LOG_ERR, "ERROR: encodeLeftThread(): pthread_setaffinity() failed, %s, exiting thread... ", strerror(errno)); 
    nThreadReturnValue = THREAD_CPU_AFFINITY_ERRORS; 
    pthread_exit((void *)&nThreadReturnValue); 
} 

CPU_ZERO(&tCPUSet); 
nReturnCode = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &tCPUSet); 
if (nReturnCode == -1) { 
    pLogFile->Write(LOG_ERR, "ERROR: encodeLeftThread(): 2nd pthread_getaffinity() failed, %s, exiting thread... ", strerror(errno)); 
    nThreadReturnValue = THREAD_CPU_AFFINITY_ERRORS; 
    pthread_exit((void *)&nThreadReturnValue); 
} 

sCPUs.clear(); 
sCPUs.append(" new left thread CPU affinity: "); 
if (bInitLog) { 
    for (int i = 0; i < CPU_SETSIZE; i++) { 
     if (CPU_ISSET(i, &tCPUSet)) { 
      sCPUs.append(" "); 
      char c = '0' + i; 
      sCPUs = sCPUs + c; 
     } 
    } 
} 
if (bInitLog) pLogFile->Write(LOG_DEBUG, "%s ", sCPUs.c_str()); 
+0

@安德烈斯·岡薩雷斯謝謝你,這是絕對有用的 – Manolete