Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 707061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:14:07+00:00 2026-05-14T04:14:07+00:00

I’ve managed to get my pthreads program sort of working. Basically I am trying

  • 0

I’ve managed to get my pthreads program sort of working. Basically I am trying to manually set the affinity of 4 threads such that thread 1 runs on CPU 1, thread 2 runs on CPU 2, thread 3 runs on CPU 3, and thread 4 runs on CPU 4.

After compiling, my code works for a few threads but not others (seems like thread 1 never works) but running the same compiled program a couple of different times gives me different results.

For example:
hao@Gorax:~/Desktop$ ./a.out
Thread 3 is running on CPU 3
pthread_setaffinity_np: Invalid argument
Thread Thread 2 is running on CPU 2
hao@Gorax:~/Desktop$ ./a.out
Thread 2 is running on CPU 2
pthread_setaffinity_np: Invalid argument
pthread_setaffinity_np: Invalid argument
Thread 3 is running on CPU 3
Thread 3 is running on CPU 3
hao@Gorax:~/Desktop$ ./a.out
Thread 2 is running on CPU 2
pthread_setaffinity_np: Invalid argument
Thread 4 is running on CPU 4
Thread 4 is running on CPU 4
hao@Gorax:~/Desktop$ ./a.out
pthread_setaffinity_np: Invalid argument

My question is “Why does this happen? Also, why does the message sometimes print twice?”

Here is the code:

#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sched.h>
#include <errno.h>

#define handle_error_en(en, msg) \
               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

void *thread_function(char *message)
{
    int s, j, number;
    pthread_t thread;
    cpu_set_t cpuset;

    number = (int)message;
    thread = pthread_self();    
    CPU_SET(number, &cpuset);

    s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    if (s != 0)
    {
        handle_error_en(s, "pthread_setaffinity_np");
    }

    printf("Thread %d is running on CPU %d\n", number, sched_getcpu());

    exit(EXIT_SUCCESS);
}

int main()
{
    pthread_t thread1, thread2, thread3, thread4;
    int thread1Num = 1;
    int thread2Num = 2;
    int thread3Num = 3;
    int thread4Num = 4;
    int thread1Create, thread2Create, thread3Create, thread4Create, i, temp;

    thread1Create = pthread_create(&thread1, NULL, (void *)thread_function, (char *)thread1Num);
    thread2Create = pthread_create(&thread2, NULL, (void *)thread_function, (char *)thread2Num);
    thread3Create = pthread_create(&thread3, NULL, (void *)thread_function, (char *)thread3Num);
    thread4Create = pthread_create(&thread4, NULL, (void *)thread_function, (char *)thread4Num);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    pthread_join(thread4, NULL);

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:14:07+00:00Added an answer on May 14, 2026 at 4:14 am

    The first CPU is CPU 0 not CPU 1. So you’ll want to change your threadNums:

    int thread1Num = 0;
    int thread2Num = 1;
    int thread3Num = 2;
    int thread4Num = 3;
    

    You should initialize cpuset with the CPU_ZERO() macro this way:

    CPU_ZERO(&cpuset);
    CPU_SET(number, &cpuset);
    

    Also don’t call exit() from a thread as it will stop the whole process with all its threads:

    exit(EXIT_SUCCESS);  
    return 0; // Use this instead or call pthread_exit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 460k
  • Answers 460k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Properties work using the descriptor protocol, which only works on… May 15, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer Try this: myVideoCaptureLayer.orientationSupported = NO; May 15, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer To call a method of a generic type object you… May 15, 2026 at 11:54 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.