CPU Affinity Masks (Putting Threads on different CPUs)

前端 未结 3 1386
孤城傲影
孤城傲影 2020-12-14 09:07

I have 4 threads, and I am trying to set thread 1 to run on CPU 1, thread 2 on CPU 2, etc. However, when I run my code below, the affinity masks are returning the correct va

3条回答
  •  攒了一身酷
    2020-12-14 09:51

    You're trying to set the affinity of threads that you did not initialize.

    Edit: Ok, let me give you some more info:

    Don't mix thread handles (the thing you store in the pthread_t variable) and what they represent (a thread of execution that runs somewhere). What you were trying to do is to set a property of a thread before it starts, with an API that requires the thread object. As it happens pthread_create creates the object and starts the execution at the same time, so trying to use pthread_setaffinity_np is not the right way to go (this is useful if you want to change the affinity of a currently running thread).

    But... pthread_create has an attribute parameter (you're passing NULL to it). This is storing the information of how you want the thread to be created.

    Affinity is one of the attributes you can set through that parameter. See the man-page documentation for pthread_attr_init and pthread_attr_setaffinity_np for how exactly

提交回复
热议问题