Scott Langham
Scott Langham

Reputation: 60311

Do thread id's always fit into a four digit decimal number?

I'm writing log statements to a file and want to prepend each line with the thread id that generated the statement.

It appears that GetCurrentThreadId function always returns a number <= 9999. So, can I assume I can always format the thread id into a four digit string? I'd like to keep the id short and a consistent length to make the log files easier to read.

GetCurrentThreadId returns a DWORD which could obviously contain big numbers.

Thanks.

Upvotes: 1

Views: 1081

Answers (4)

Adriano Repetti
Adriano Repetti

Reputation: 67090

Short answer: no.

Actually for 32 bit applications the (virtual) limit is around 2k threads. For 64 bit applications it's around 14k threads.

Read this article as quick reference: https://learn.microsoft.com/en-us/archive/blogs/markrussinovich/pushing-the-limits-of-windows-processes-and-threads

This is the limit for the total number of threads (it's primary a problem of memory because of the stack of each thread) but there is not any rule about how thread ID is assigned (if your application creates a lot of short living threads then you may overflow this assumption).

Upvotes: 3

Wolf5370
Wolf5370

Reputation: 1374

Why not write it as hex it will fit nicely in 8 digits then?

//Edit (Can't count!)

Upvotes: 3

Joey
Joey

Reputation: 354516

Give it another character space. It won't kill you and thread IDs are, as you mention, a DWORD and thus could be larger.

Upvotes: 1

Jay
Jay

Reputation: 24895

The MSDN page of GetCurrentThreadID says:

the thread identifier uniquely identifies the thread throughout the system.

So the thread id is unique across the system and not just your process, in which there is a good possibility that, it might at some time, return a value greater than a 4 digit decimal number.

Upvotes: 3

Related Questions