pthread_create tid?

This is a discussion on pthread_create tid? within the C Programming forums, part of the General Programming Boards category; Hi, I got a problem with pthread_create code: Code: pthread_t tid1; pthread_create(&tid1, NULL, thread1, (void *)tst); printf("tid: %d\n", tid1); According ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    47

    pthread_create tid?

    Hi, I got a problem with pthread_create

    code:

    Code:
    pthread_t tid1;
    pthread_create(&tid1, NULL, thread1, (void *)tst);
    printf("tid: %d\n", tid1);
    According to the man page of pthread_create the tid gets stored in tid1, but when I try and print it out I only get like -1204387023 something, instead of the correct tid of the thread I just created?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you sure that the large negative number ISN'T the tid that you just created - what says that it has to be a small integer? Although, I have to admit that the hex version makes just as little sense: 0xB8368331

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    47
    Im sure, when I print out the tid from within the new thread, using:

    Code:
    int i = syscall(SYS_gettid);
    I get a low nice nr like 4506

    Thing is I need this tid in the main thread when I create the child thread.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    47
    Interesting ... if I reserver memory for the tid1 I get a positive BIG number AND its the same every time I run the program, while tid2 gets a BIG negative number, which is different every time??

    Code:

    Code:
    pthread_t *tid1, tid2;
    
    tid1 = (pthread_t *)malloc((sizeof(pthread_t)));
    pthread_create(tid1, NULL, thread1, (void *)tst);
    printf("tid1: %d\n", tid1);
    
    pthread_create(&tid2, NULL, thread2, (void *)tid2);
    printf("tid2: %d\n", tid2);

    gives:
    tid1: 134529032
    tid2: -1218405488

    tid1: 134529032
    tid2: -1218954352

    tid1: 134529032
    tid2: -1218651248

    Anyone that can explain for me? I want the tid of the thread I'm creating, when I create it. Can I pick it out in someway of this values?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you want to print *tid1 instead? Right now you are printing the address of your thread ID, which is pretty much guaranteed to be different from the ID itself - I suspece the ID itself is an address to some sort of internal data-structure - but it's probably not visible to you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    47
    some sort of internal data-structure - but it's probably not visible to you
    Thats correct.
    A well have to use gettid() from within the threads then ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get tid of kernel threads?
    By micke_b in forum C Programming
    Replies: 4
    Last Post: 10-19-2007, 08:25 AM
  2. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 12:41 PM
  3. Linked List question from a newb(Need code Critique)
    By Gatt9 in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2006, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21