0

I came across this line of code today in process and thread chapter of C programming book:

printf("[Child]  child thread id: 0x%x\n", (unsigned int)pthread_self());

I've never seen the part (unsigned int)pthread_self(), I don't know what the first pair of parenthesises is used for. Any idea?

p.s:

I remember that in the documentation of php, there's similar expression for function documentation:

int time()

but in actual code, we only use the part time(), int is for documentation purpose to show the return value of function time()


Update:

I type the example code in the book that test the each thread id:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>

int global = 5;

void* ChildCode(void* arg) {
    int local = 10;

    global++;
    local++; 
    printf("[Child]  child thread id: 0x%x\n", (unsigned int)pthread_self());
    printf("[Child]  global: %d  local: %d\n", global, local);

    pthread_exit(NULL);
}

int main() {
  pthread_t  childPid;
  int       local = 10;

    printf("[At start]  global: %d  local: %d\n", global, local);

  /* create a child thread */
  if (pthread_create (&childPid, NULL, ChildCode, NULL) != 0)
  {
    perror("create");
    exit(1);
  } else { /* parent code */
    global++;
    local--; 
    printf("[Parent] parent main thread id : 0x%x\n", (unsigned int)pthread_self());
    printf("[Parent] global: %d  local: %d\n", global, local);
    sleep(1);
  }
  printf("[At end] global: %d  local: %d\n", global, local);
  exit(0);
}

and it gives me some note(not warning not error):

clang example_thread.c 
/tmp/example_thread-9lEP70.o: In function `main':
example_thread.c:(.text+0xcc): undefined reference to `pthread_create'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I've no idea with the code, any idea?

eckes
  • 64,417
  • 29
  • 168
  • 201
mko
  • 21,334
  • 49
  • 130
  • 191
  • You need to pass `-pthread` to clang. See http://stackoverflow.com/questions/2391194/what-is-gs-pthread-equiv-in-clang – ecatmur Aug 07 '12 at 10:21

4 Answers4

2

The return value of pthread_self() is a pthread_t (see man pthread_self).

(unsigned int) pthread_self() is used to cast the return value of pthread_self() into an unsigned integer.

For more information about casting in C see http://www.aui.ma/personal/~O.Iraqi/csc1401/casting.htm

eckes
  • 64,417
  • 29
  • 168
  • 201
1

This is just casting of the return value of the function to unsigned int.

It is just like:

pthread_t pthread_self_result;
pthread_self_result = pthread_self();
printf("[Child]  child thread id: 0x%x\n", (unsigned int)pthread_self_result);
MByD
  • 135,866
  • 28
  • 264
  • 277
  • thanks for your quick reply, and your example code, so the casting the value is for printf use, isn't it? – mko Aug 07 '12 at 10:03
  • Could you help me to check the error message which I update for the question, the code won't compile – mko Aug 07 '12 at 10:17
1

The parentheses are casting the return value of pthread_self to unsigned int. pthread_self returns pthread_t, which is an unspecified arithmetic type that is not appropriate for use with printf.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • `pthread_t` need not be an arithmetic type or even a scalar type, e.g. it could be a huge structure. This would be really inefficient, but it's allowed, and thus code containing this cast is invalid. The only way to portably print `pthread_t` is to print its representation as `unsigned char[sizeof(pthread_t)]`. – R.. GitHub STOP HELPING ICE Aug 07 '12 at 13:29
  • 1
    And if you are going to assume `pthread_t` is a scalar type, you should still use `uintmax_t`, not `unsigned int`. The latter is too short to represent all values of `pthread_t` on 64-bit systems. – R.. GitHub STOP HELPING ICE Aug 07 '12 at 13:30
1

This is what is called in C a "type cast" Here we cast the pthread_t type into an unsigned int to print it. Please refer to your C language manual

Stephane Rouberol
  • 4,286
  • 19
  • 18
  • +1 for pointing out the term "type cast". And sorry for this noobie question, I should thoroughly learn the C reference, but it's not easy for a noob like me, but I'm trying – mko Aug 07 '12 at 10:14