RTC222
RTC222

Reputation: 2323

pthreads - set cpu affinity for two threads to one core

The following C code for Linux initalizes N threads with one thread per core. The threads are created and affinity set in the first for-loop. Thread 0 (in the section "if i == 0") is assigned to call Thread_Process2 and all other threads (in the section "if i > 0") are assigned to call Thread_Process1. I've used this program for some time with great success.

Now I want to create two threads on core 0 and one thread on all other cores. I think I can create the extra thread on core 0 by adding the extra pthread_create line I added in the section "if i == 0" but where we call pthread_join in the second for-loop the threads are identified by threads[i] but two threads share the same core number and therefore are not identified separately in order to be joined.

Here's the code:

int thread_create_in_C_FC(numberOfProcessors) {

    pthread_t threads[numberOfProcessors];

    pthread_attr_t attr;
    cpu_set_t cpus;
    pthread_attr_init(&attr);

    for (int i = 0; i < numberOfProcessors; i++) {
       CPU_ZERO(&cpus);
       CPU_SET(i, &cpus);
       printf("Core created %d\n", i);
       pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpus);

    if (i > 1){
       printf("Thread created %d\n", i);
       pthread_create(&threads[i], &attr, Thread_Process2, NULL); }

    if (i == 0){
       printf("Final thread created %d\n", i);
       pthread_create(&threads[i], &attr, Thread_Process1, NULL);
       pthread_create(&threads[i], &attr, Thread_Process1, NULL);}
    }

    for (int i = 0; i < numberOfProcessors; i++) {
        pthread_join(threads[i], NULL);
        printf("Core joined %d\n", i);
    }

    return numberOfProcessors;
}

My questions are:

  1. Is it correct that I can create two threads on core 0 by adding the extra pthread_create line in the section "if i == 0" ?

  2. If so, how do I join both threads on core 0 in the second for-loop where we call pthread_join?

Upvotes: 1

Views: 987

Answers (1)

RTC222
RTC222

Reputation: 2323

Here's how I solved this problem. So far there do not appear to be any problems with doing it this way:

int thread_create_in_C_FC(numberOfProcessors) {

    pthread_t threads[numberOfProcessors+1];

    pthread_attr_t attr;
    cpu_set_t cpus;
    pthread_attr_init(&attr);

    for (int i = 0; i < numberOfProcessors; i++) {
       CPU_ZERO(&cpus);
       CPU_SET(i, &cpus);
       printf("Core created %d\n", i);
       pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpus);

    if (i > 0){
       printf("Thread created %d\n", i);
       pthread_create(&threads[i+1], &attr, Thread_Process2, NULL); }

    if (i == 0){ //Core 0 Thread 1
       printf("Final thread created %d\n", i);
       pthread_create(&threads[i], &attr, Thread_Process1, NULL);
       pthread_create(&threads[i+1], &attr, Thread_Process1, NULL);}
    }

    for (int i = 0; i < numberOfProcessors+1; i++) {
        pthread_join(threads[i], NULL);
        printf("Core joined %d\n", i);
    }

    return numberOfProcessors;
}

At several points above I added 1 to the count, as indicated by the color-coding you see if you're on Stack Overflow dark mode.

If I find any problems after working with it, I'll update this answer. But it looks like the correct way to do it because now I get the two threads assigned to core 0 and others assigned to higher core numbers.

Upvotes: 1

Related Questions