0

我试图找出在 HT 系统中创建 pthread 时,Scientific Linux Kernel 2.6.32 遵循的默认关联策略是什么。有没有办法知道?在 pthreads 创建中没有调用亲和力,所以我猜亲和力留给操作系统。

4

1 回答 1

1

不确定这是否有帮助,但这里有一些代码将使用 pthread 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());
于 2014-01-31T02:16:28.510 回答