Solved

Calling Exit () functio within a Multithreaded Environment

Posted on 2008-06-13
2
282 Views
Last Modified: 2013-12-04
I am writing a multithreaded Windows application in C using Visual Studio.net.  In certain situations, my application needs to shut down immediately, and I was thinking of calling the exit() function within the main thread.  Will this have any adverse effects on the system it is running on?  Are there any safer methods that I can use (remembering that it needs to shut down as quickly as possible)?
0
Comment
Question by:jtrades
2 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 21781706
Calling exit (in Windows) will end the process after doing some basic cleanup (like stopping the threads etc.). There's no adverse effects on the system (other than the fact that the process won't be running any more ;) ).


I would however recommend to shut down your threads properly, or you might halt them in the middle of something which might or might not be bad (depending on what the threads do).
0
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 125 total points
ID: 21781890
Hi jtrades,

Infinity's right.  :)

Keep in mind the differences between a program and a thread.  A program is just what you think it is -- you run Notepad.exe, MyApplication.exe, etc. and the program occupies a memory space, uses system resources, and interacts with the user and the operating system.

A thread is a place (function or routine ) within a program that is that is identified to the operating system as another place for the CPU to execute code, outside of the main program.

exit () terminates the program.  It doesn't know about or care about threads, so results in the fastest shutdown.  The underlying operating system certainly knows about the program's threads and will take whatever steps necessary to dequeue the threads prior to the program's termination.

So the issue is cleanup.  If you've got a multi-threaded program where the threads are playing chess against each other, you may not care how gracefully the threads clean up.  But if you've got a multi-threaded program that is essentially a database manager, you'd better clean up gracefully to ensure that you're not in the middle of a series of write operations as writing only part of the series would result in a corrupt database.


Good Luck,
Kent
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If your system is showing symptoms of browser hijacks or 'google search redirects' check out my other article (http://rdsrc.us/u3GP7A) first and run the tool TDSSKiller (http://rdsrc.us/GDBBs4) to get rid of the infection. Once done, and if the …
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question