1

Say I am writing a pthread wrapper. I try to assign unique ids to threads in thread creation phase so that I can make the execution deterministic. How can I let each thread know his unique id?

onesuper
  • 196
  • 2
  • 7

1 Answers1

1

You cannot force the system to use an id of your chosing.

As per @Lazin comment: pthread_self() will tell the pthread the id the system has given it.

If you want to allocate your own id (eg some small integer) then clearly you can do that and pass it into the pthread with any other args passed using the void* arg argument (the last argument) of pthread_create().

  • I am working on a deterministic multithreading project. Briefly speaking, I want to use a token to make sure at any time there is only one thread is executing. So I hope a thread check whether he holds the token (token_id == myid) when he arrives at the critical section. That is I should assign a unique id to each thread when spawning them (in thread_creation wrapper). – onesuper Jul 22 '14 at 14:00
  • OK. Well, the thread-id that is returned when the thread is created is the same as the thread-id returned to the thread by `pthread_self()`, and you can compare thread-ids for equality (`pthread_equal()`), and they are unique... so you could use the `pthread_t` as you `token_id` ? –  Jul 22 '14 at 14:43