How to print out thread id from pthread_t
Language/C 2012. 4. 6. 17:40Frank Cusack wrote On 06/18/07 12:28,:
> On Mon, 18 Jun 2007 15:51:08 -0000 Bin Chen
>
>>On Jun 18, 11:44 pm, Herman.Schu...@gmail.com wrote:
>>
>>>Hi,
>>>
>>>I am trying to print out the thread id in my program.
>>> static pthread_t GetCurrentThreadID() { return :thread_self(); }
>>>
>>>void myFunction() {
>>>pthread_t id = GetCurrentThreadID();
>>>
>>> printf (" thread id %d\n", id);
>>>
>>>}
>>>
>>>But I get a negative number: -122112944. Did I do something wrong?
>>>Why the number is negative?
>
>
> Because %d is an integer type and prints both positive and negative
> numbers. The thread id happens to be very large and is treated as a
> negative integer.
>
>
>>The pthread_t is defined as an unsigned variable,
>
>
> No it isn't. It's an opaque type.
>
> printf("%lu\n", (unsigned long) id);
And even that won't always work: pthread_t is *really*
opaque, and need not be a type to which a cast operator can
be applied. On at least one implementation, pthread_t is
a struct.
--
Eric.Sosman@sun.com
- http://fixunix.com/unix/84437-how-print-out-thread-id-pthread_t.html
'Language > C' 카테고리의 다른 글
Why use select() instead of sleep()? (0) | 2012.04.26 |
---|---|
아스키 코드 0x00 ~ 0x20 까지 (0) | 2012.04.10 |
Programmiersprache C/C++ (0) | 2012.03.16 |
getprocs64() (get process information on aix) (0) | 2012.03.16 |
fork, wait, waitpid, zombie(좀비) (0) | 2012.03.15 |