[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Threads

If I start a new thread with _beginthread(...)
do I have to terminate it before I exit application,
or it is automatically terminated?
0
bor061297
Asked:
bor061297
  • 2
1 Solution
 
SlartiCommented:
What OS are we talking about here?
It sounds like Unix, which means I don't know, but in Windows the rule is that you don't have to, but you should.
0
 
alexoCommented:
The process (the "application" as you call it) will not terminate until all the threads are done.  Therefore, yes, you have to terminate the thread.  However, the termination is as simple as returning from the thread function, you don't have to explicitly call _endthread.

An advice: _beginthreadex() is a more robust way of starting a thread (but in requires closing the thread handle).

0
 
bor061297Author Commented:
I am talking about Win32.
When I click 'X' application terminates, even if
i didn't closed all thread, but I am worried if
some thread stayed active after closing application.
0
 
alexoCommented:
>> When I click 'X' application terminates, even if i didn't closed all thread

Closing an application with the 'X' sends a WM_CLOSE message to the main window.  Closing the main window and exiting the message pump does not mecessarily mean that the process is no longer running.

Use the ExitProcess() API to exit all the threads of your process if you want to be sure.

Of course, the threads usually do something important at the time so it is better to notify them that the program is pending termination (by setting a global flag which they poll, for instance) and then WaitForMultipleObjects() on the threads.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now