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 6827085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:13:28+00:00 2026-05-26T22:13:28+00:00

struct thread_info { int id; pthread_t thread_id; int thread_num; int gpu; }; struct thread_info

  • 0
struct thread_info
{
    int       id;
    pthread_t thread_id;
    int       thread_num; 
    int       gpu;
};

struct thread_info *tinfo;

static void *GPUMon(void *userdata)
{
    struct thread_info *mythr = userdata;
    const int thread = mythr->id;

    while(1)
    {
        printf("Thread: %d\n", thread);
        Sleep(4000);
    }

    return NULL;
}

int main(void)
{
        struct thread_info *thr;

        tinfo = calloc(2, sizeof(*thr));
        for(int ka = 0; ka < 2; ka++)
        {           

            thr = &tinfo[ka];
            thr->id = ka;

            if (pthread_create(thr, NULL,GPUMon, thr))
            {
                return 0;
            }
        }   

                // some more code


    return 0;
}

Basically, the GPUMon thread should print the ID of my thread which is thr->id = ka; however what I get are enormously huge numbers such as

Thread: 7793336
Thread: 7792616

I do not understand what I am doing wrong.

  • 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-26T22:13:29+00:00Added an answer on May 26, 2026 at 10:13 pm

    pthread_create() takes a reference to pthread_id as its first argument.

    So (as a quick&dirty fix) I’d propose you change struct thread_info to be:

    struct thread_info
    {
      pthread_t thread_id;
      int       id;
      int       thread_num; 
      int       gpu;
    };
    

    Or (much nicer) do call pthread_create() like this:

    if (pthread_create(&thr->thread_id, NULL, GPUMon, thr))
    {
      return 0;
    }
    

    The results your solution prints out are the values for the pthread_id assigned by pthread_create() (or at least parts of it depending on your platform) which have accidently been written into the member id of struct thread_info as you passed a pointer to the wrong structure.

    And to say it again: At least when developing do turn all compiler warnings on as (also in this case they would have) they could point you to your mistake(s) (gcc option -Wall)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up the following struct: typedef struct _thread_node_t { pthread_t thread; struct
I have the following C code: static void* heap; static unsigned int ptr; int
Is a C# struct thread-safe? For example if there is a: struct Data {
struct { char a; int b; } x; Why would one define a struct
struct SomeStruct { int a; int b; }; SomeStruct someFn( int init ) {
struct Div { int i; int j; }; class A { public: A(); Div&
struct struct0 { int a; }; struct struct1 { struct struct0 structure0; int b;
struct Drink { public string Name { get; private set; } public int Popularity
struct ID3v2_header{ char tag[3]; char ver[2]; char flag; int size; }; ID3v2_header readID3v2_head(string file){
#include <iostream> #include <boost/thread.hpp> using std::endl; using std::cout; using namespace boost; mutex running_mutex; struct

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.