[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Calling Exit () functio within a Multithreaded Environment

Posted on 2008-06-13
2
Medium Priority
?
299 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 500 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 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 500 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

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!

Question has a verified solution.

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

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 …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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.
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.

873 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