Community
cancel
Showing results for 
Search instead for 
Did you mean: 
73 Views

How to check if thread is a main thread.

Hi,

I would like to distinguish main thread and workers threads within function. How can i check if current thread is a main thread ? (WinAPI). I'm playing with TLS and i want to treat main thread in different why than others.

TIA,
M
0 Kudos
3 Replies
73 Views

Problem solved. The most obvious solution is the most difficult to find. I store thread id in global variable. Then I refere to it each time i need to check whether main thread called my function or not. I'm ashamed.

M
jimdempseyatthecove
Black Belt
73 Views


You can either use the thread ID or use something like "bool isMasterThread"
As long as you are storing the thread ID in TLS you might consider storing a thread sequence number (0,1,2,...)
This is a global sequence number. Use InterlockedIncrement on a static counter and store in TLS. Note, The OpenMP omp_get_thread_num() returns the thread number within the team of thecurrent parallel region. When Nested off or at nest level 0 the team member numbers == the thread sequence number. However, when at a nested level, each team uses (0,1,2,...) within their team (0==thread that spawned the current team).

Jim Dempsey

73 Views

Actually, i'm developing application which uses mysql db. I want to increase it's responsiveness by moving time-consuming fetches to other threads. In effect, each thread needs its own connection to database.
I try to make it as much transparent as possible, so i've got one function which returns "pointer" to database. Thanks to TLS it may return new connection for each worker thread. In that case, order is meaningless. Neverthless, its worth to keep your hint in mind. As novice at TLS, i found it briliant.

BTW, when i examined my problem one more time, i realized that i even do have to distinguish between main thread and workers one. I wanted to keep main thread always connected to database (opposite to workers), but i found that smart-pointers will do it for me.


M
Reply