0

I would like to get the thread ID in a numeric form with the following code:

#include <string.h>
...
pthread_t ptid = pthread_self();
unsigned long long threadId = 0;
memcpy(&threadId, &ptid, std::min(sizeof(threadId), sizeof(ptid)));
    

But I get this error:

error: ‘std’ undeclared (first use in this function)
   17 |     memcpy(&threadId, &ptid, std::min(sizeof(threadId), sizeof(ptid)));
      |                              ^~~

How can I fix that?

mahmood
  • 23,197
  • 49
  • 147
  • 242
  • 1
    Copying (the first bytes of) a `pthread_t` to (the first bytes of) an `unsigned long long` is inherently non-portable. The thread ID does not need to be an arithmetic type, it can be a structure, see https://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html#tag_13_67_10 Even if it is an arithmetic type, the result of the `memcpy` depends on your system's byte order. What do you want to achieve with this assignment? See also https://stackoverflow.com/q/1759794/10622916 – Bodo Mar 20 '23 at 12:51
  • Please see the new topic at https://stackoverflow.com/questions/75790653 – mahmood Mar 20 '23 at 13:06
  • Please [edit] your question and add all requested information or clarification to the question. As I understand the other question you want to have an index in range `0`..`(n-1)` for the `n` threads you create. A [comment](https://stackoverflow.com/questions/75790653/getting-logical-thread-id-number#comment133694857_75790653) to this question already suggests to use the index `i` you use for your arrays that store the thread IDs and args as your logical thread ID. – Bodo Mar 20 '23 at 13:52

1 Answers1

2

std::min is for C++.

Instead of that, you should define a function to obtain the smaller (not larger) value by yourself and use that.

size_t min_size(size_t a, size_t b) {
    if (a <= b) {
        return a;
    } else {
        return b;
    }
}
memcpy(&threadId, &ptid, min_size(sizeof(threadId), sizeof(ptid)));
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Right. But if I use that and then `printf("tid=%d\n", threadId);` I see `tid=-300751296`. – mahmood Mar 20 '23 at 12:38
  • @mahmood You declared `threadId` as type `unsigned long long`, so use `printf("tid=%llu\n", threadId);` to print it. – Ian Abbott Mar 20 '23 at 12:41
  • Then it would be `tid=139713351722560`. In the main(), I have defined `pthread_t threads[4];` – mahmood Mar 20 '23 at 12:42
  • @mahmood The large integer value is probably because `pthread_t` is really representing a pointer value in your system's Pthreads implementation. Also, by using `memcpy` when the sizes of `pthread_t` and `unsigned long long` could be different, you are really assuming that the implementation uses a little-endian representation of integers. Better to use a union for type-punning. – Ian Abbott Mar 20 '23 at 12:52
  • Sorry, I didn't get the point. I didn't think the getting the thread ID inside the thread function would be so challenging. Would you provide a snippet for that please? – mahmood Mar 20 '23 at 12:56
  • @mahmood Actually, type-punning via a union wouldn't necessarily work properly when the types being punned are different sizes, so you can ignore that bit. – Ian Abbott Mar 20 '23 at 13:03
  • OK I created a new topic with the specific problem I have. https://stackoverflow.com/questions/75790653 – mahmood Mar 20 '23 at 13:06