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

bause@exunido.uucp (Falko Bause) (10/18/88)

COROUTINE CONCEPT IN C++
========================

Thanks for all the mails I got.
Most of the mails refer to task.h where procedures
for event scheduling simulation are declared (is that
correct? ; the documentation for task.h isn't available
to me at the moment).
I also got one message by

thomas%spline.uucp@utah-gr.UUCP (Spencer W. Thomas)
(Newsgroups: comp.unix.wizards
Subject: Re: A very interesting problem -- multiprocessing)

with C-source code implementing the SIMULA-like procedures 
detchach and resume. It is a not fully tested version
running on PDP-11 architecture and not portable.

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.


E.g. take the following code which represents the
process of a "worker":



void worker();
{
  while (1)
  {
     goto_work();
     hold(4); // time scale e.g. in hours
     goto_lunch();
     hold(0.5);
     go_back_working();
     hold(4);
     go_home();
     passivate(); // wait until ringing bell activates worker
  }
}



This process can be implemented in nearly every
language by storing the entry point into the
procedure for the next "resume".

void worker();
{
  switch (status_of_worker:) // indicating the present status of the worker
                             // it therefore represents implicitly the entry
                             // points of the procedure
  {
  case at_home:  	  status_of_worker = before_lunch;
  			  goto_work(); hold(4); break;
  case before_lunch:  	  status_of_worker = lunch;
		          goto_lunch(); hold(0.5); break;
  case lunch:             status_of_worker = after_lunch;
			  go_back_working(); hold(4); break;
  case after_lunch:       status_of_worker = at_home;
                          passivate(); break;
  }
}

This function is called by other "process procedures" activating
the worker.
This example shows that process interaction can be implemented in
every language leaving the handling of the entry points to the
programmer.

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++?


Falko

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;
}