LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I delete a thread?

I'm coding in Visual (2017) C, and the task seems simple enough.

 

I am using CmtReleaseThreadPoolFunctionID to close threads.  I haven't been calling CmtWaitForThreadPoolFunctionCompletion beforehand since I'm usually only closing threads inside SignalCommThreadToStop, a function that was made to wait for the thread to exit its functions, (although I wasn't the one who wrote it).

 

However, even after CmtReleaseThreadPoolFunctionID I still see the thread in the thread list and Parallel Stacks.  Perhaps it's just my inexperience with multithreaded apps and C, but shouldn't unloaded threads not show up anymore? 

 

I looked up how to kill threads normally in C, and found the answer was pthread.h; however, pthread.h is not native to Windows, and I don't want to install something new at this stage in the project.  (Not to mention it doesn't make sense that pthread.h would be the only way to close threads in C.  How was pthread.h created without pthread.h?)

 

Finally, another person online (not working in Lab Windows) said that I shouldn't be closing threads at all and that I should put them in a loop.  This doesn't make any sense at all to me, as once CmtReleaseThreadPoolFunctionID is called, the thread won't be used again, and as such any remnants of the thread are just wasting memory, and seemingly processing time; as even "dead" threads that appear to no longer be in use, will change location.

 

My program typically accumulates over 20 dead threads during startup, and I think it might be causing performance issues.  How do I remove them from memory?

 

P.S. Almost all these threads are related to WiFi connectivity, and no I do not have time to rewrite the program to not use as many threads.

0 Kudos
Message 1 of 2
(1,091 Views)

Here's a snapshot of my program's threads, from the thread viewer.  The second flagged thread should have just been released.

 

       Flagged > 12024 0 Main Thread Main Thread SunConfig.exe!SignalCommThreadToStop
Not Flagged 28316 0 Worker Thread ntdll.dll thread ntdll.dll!775047dc
Not Flagged 22516 0 Worker Thread ntdll.dll thread ntdll.dll!775047dc
Not Flagged 12020 0 Worker Thread ntdll.dll thread ntdll.dll!775047dc
Not Flagged 22412 0 Worker Thread winmm.dll thread win32u.dll!761411dc
Not Flagged 14264 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 11548 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
      Flagged 14280 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 25044 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 17988 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 25668 0 Worker Thread cvirte.dll thread win32u.dll!7614586c
Not Flagged 15020 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 11632 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 3044 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 26544 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 6152 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 25984 0 Worker Thread cvirte.dll thread win32u.dll!7614586c
Not Flagged 17464 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 14840 0 Worker Thread cvirte.dll thread win32u.dll!7614586c
Not Flagged 16872 0 Worker Thread cvirte.dll thread win32u.dll!7614586c
Not Flagged 26748 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 13132 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 17456 0 Worker Thread cvirte.dll thread cvirte.dll!791569ac
Not Flagged 26968 0 Worker Thread ntdll.dll thread ntdll.dll!775047dc
Not Flagged 2876 0 Worker Thread ntdll.dll thread ntdll.dll!775047dc
0 Kudos
Message 2 of 2
(1,086 Views)