Solved

Calling Exit () functio within a Multithreaded Environment

Posted on 2008-06-13
2
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

718 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