Skip to content
Advertisement

C – pthread_self() getting truncated first 4 bytes?

I’m having trouble getting pthread_equal() to match stored pthreads. The pthread_t I get from pthread_self() seems to be truncated by 4 bytes compared to the one I get from pthread_create(). Comparisons are in the last code block!

JavaScript

As a result, an attempt to loop through the stored pthreads and compare with pthread_equals fail. Some comparisons (all ten, every time, get truncated the first 4 bytes):

JavaScript

Any idea as to why this is happening?

Advertisement

Answer

this function body is the root of the problem:

JavaScript

the pthread_t does not have the same size as an ‘size_t’ and the code is using sizeof(i) to print the bytes of a pthread_t. suggest:

JavaScript

although a better method, not using a loop, would be:

JavaScript

caveat: the following code does not compile cleanly. It outputs a warning message about the unused parameter abc

So, with a ‘improved’ function: printThreadID() the program that produced the output is:

JavaScript

Here is the output of a typical run of the program, note the exact order of the outputs is depending on which thread wins the race.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement