Appendix N - Detecting End of Simulations
Often it is useful to detect the end of a simulation for purposes of performing certain clean-up or close-up operations such as, closing trace files, or printing summary information. Summary information such as maximums and means can only be produced at the end of a simulation, and in the case of time/based averages, -can only be produced when the end-time is known.
- CSIM_EndOfSim Synchron
All CSIM simulations automatically contain a predefined synchron variable called CSIM_EndOfSim, which your summary or clean-up threads can wait on. When there are no further events to be processed, or in other words, the inevitable end of a simulation is reached, the simulation kernel automatically calls RESUME( &CSIM_EndOfSim, NONQUEABLE );. This will unblock any clean-up/summary threads you might have waiting on that synchron.
Example Usage:
DEFINE_THREAD: summary
{
WAIT( &CSIM_EndOfSim, NONQUEUABLE );
fprintf(outfile,"Mean = %f\n", total / CSIM_TIME );
fclose(outfile);
}
END_DEFINE_THREAD.
Note that this method applies to simulations having a definite end of events. Other simulations which have no definite ending, -which produce events forever-, will never activate the CSIM_EndOfSim synchron. Such simulations may not find it useful unless they have some other stopping condition in which a user-thread explicitly resumes the thread. (It is a global synchron variable, so technically, any model could resume it. In this case, any other previously scheduled events will be removed from the event queue.) - Registering a Simulation Exit Function
Another alternative is to register a special function to be called whenever the simulation exits. Simulation exits include, whenever the user presses the exit button or clicks File/Exit in the graphical simulation, or types q, or quit in a textual simulation. Registered exit functions will also be called when the simulation ends under the -batch mode in either graphical or textual simulations.
This method is useful for simulations which run indefinitely. For example, it allows your summary or closing routines to be called whenever you press the exit button.
See: CSIM_AddExitFunc for more information.
