Is it possible to assign a name to a thread created with _beginThread
? I do not have a choice in using this function, but i am trying to keep an eye on which threads are shutting down, which can get confusing when i don't have a name associated.
Asked
Active
Viewed 1,267 times
1
-
See http://stackoverflow.com/questions/10121560/stdthread-naming-your-thread and http://stackoverflow.com/questions/3342159/give-a-name-to-a-boost-thread – Jonathan Wakely May 10 '13 at 16:41
1 Answers
4
Is this what you're looking for:
It works by raising and trivially handling a very particular exception, 0x406D1388, which is known to the debugger. The debugger retrieves the name during first-chance handling and shows that name in the Threads window.

Ben Voigt
- 277,958
- 43
- 419
- 720
-
Well, i'm not sure. I've seen that posted, but it requires a threadId. I don't know if beginthread takes in, or returns an Id. I know that it returns an int, but i don't know if that is considered its id or not. – Jason May 10 '13 at 16:45
-
@Jason: Can you set the name from inside the thread? Then you just pass `-1` as the example shows, meaning "current thread". – Ben Voigt May 10 '13 at 16:45
-
1@Jason: And since you asked, no the return value from `_beginthread` is not a thread id. It's a handle, and the [`GetThreadId(HANDLE)`](http://msdn.microsoft.com/en-us/library/ms683233.aspx) function can then be used to get the id. – Ben Voigt May 10 '13 at 16:48
-
Okay, this is probably a stupid question, but do i have write the setThreadName function (copy it from the example), or does it already exist? – Jason May 10 '13 at 17:53
-
1You should copy it from the example, and make any changes you need (for example, if you're changing the name of another thread, you'd need to pass in the handle or id). – Ben Voigt May 10 '13 at 18:29
-
1And use `_beginthreadex`, not `_beginthread`. The thread created by `_beginthread` closes the returned handle when it runs to completion, so use of that handle in the creating thread may lead to racy code (e.g., the created thread may run to completion and close the handle before `_beginthread` returns to its caller on the creating thread). – James McNellis May 10 '13 at 23:04