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.
DEFINE_THREAD: summaryNote 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.)
WAIT( &CSIM_EndOfSim, NONQUEUABLE );
fprintf(outfile,"Mean = %f\n", total / CSIM_TIME );
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.