how can I tell if pthread_self is the main (first) thread in the process?

前端 未结 3 1890
猫巷女王i
猫巷女王i 2021-01-11 16:59

background: I\'m working on a logging library that is used by many programs.
I\'m assigning a human-readable name for each thread, the main thread should get \"main\", b

3条回答
  •  走了就别回头了
    2021-01-11 17:26

    You can utilize some kind of shared name resource that allows threads that spawn to register a name (perhaps a map of thread id to name). Your logging system can then place a call into a method that gets the name via the thread ID in a thread-safe manner.

    When the thread dies, have it remove it's name from the mapping to avoid leaking memory.

    This method should allow all threads to be named, not just main.

提交回复
热议问题