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:
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" ?
If so, how do I join both threads on core 0 in the second for-loop where we call pthread_join?