[comp.lang.c] Coroutines in C++

mbb@trotter.usma.edu (MAJ Mark B. Bilodeau) (10/23/88)

In <657@laura.UUCP> Falko Bause writes:

> To my mind the procedures in task.h are not very suited
> to a process interaction style of simulation, which is,
> I think, the best style for simulation.
> 
> On the other hand it is always possible to implement process interaction
> in languages which don't support the use of coroutines.
> I've done this in PASCAL some years ago.

[ Process worker example omitted for brevity ]

> This example shows that process interaction can be implemented in
> every language leaving the handling of the entry points to the
> programmer.

But this is cumbersome way of programming a simulation.  A programmer
coding the interactions of a dynamic system has more than enough to
worry about without the details of how process interactions are
handled.

> My first question (if there is a coroutine concept in C++) therefore
> asks for a possibilty to write the procedures for processes in
> a SIMULA-like style like the first version of worker().
> 
> Is that possible in C++?

As I recall (and its been a while since I looked at using c++ for
simulation work) the handling of simulation time and sequencing sets
(SQS in SIMULA (?)) did not strike me as terribly straight forward.
As has been previously mentioned the whole language was a bit more
than I felt I could burden my simulation students with.  Also it did
not support the commonly used math functions (negexp, draw, norm ...).

What I really wanted to use was SIMULA.  Actually, I didn't need all
of SIMULA just the concept of a process and the process sequencing
functions.  Well on thing led to another and I did develop the
language I call "csim", a combination of SIMULA process with the C
language.  Csim is implemented as a preprocessor for cc and runs
on the 3b2 series.  It appears to be relatively transportable
except for 2 functions suspend and resume that manipulate the
stacks.  Unfortunately this is very machine and compiler dependent.
The following program is the "Sieve of Eratosthene" example from
"Simula Begin" (?) in csim.  The analogy is not perfect but it has
been useful as a teaching vehicle.

	Mark
================================== eratosthene.c ===============================
#include	<csim.h>

%%				<-- Removed by preprocessor

	int	limit;
	int	n;
	int	pos = 0;
	process	sieve *sv;

main()
{
	simtrace = 0;		<-- Turn off built in tracing
	printf("Enter limit => ");
	scanf("%d",&limit);
	printf("Prime Numbers Between 1 and %d\n\n",limit);
	printf("%10d",1); pos = 10;
	n = 1;
	sv = new sieve(3);
	activate sv at 3.0;
	reactivate current at limit;
	printf("\n\n");
	printf("The number of prime numbers is %d\n",n);
	exit(0);
}

process sieve(p)
	int	p;
{
	n = n + 1;
	if (pos >= 70) { printf("\n"); pos = 0; }
	printf("%10d",p); pos += 10;
lab:
	if (((SQEP)current->sqe->succ)->time - simtime > 2.0) { <-- This is
	    sv = new sieve((int)simtime+2);			    terribly
	    activate sv delay 2.0;				    UGLY but
	}							    the only
	hold p * 2.0;						    way!!
	goto lab;
}