Solved

Calling Exit () functio within a Multithreaded Environment

Posted on 2008-06-13
2
249 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:Kdo
Kdo 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now