• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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