Skip to content

Do several things at once

cl4cnam edited this page Dec 12, 2023 · 7 revisions

In this tutorial, we are going to do several things in the same time.

Prepare your environment

Do as in Hello World tutorial except that you change the names: severalAtOnce, severalAtOnce.html, severalAtOnce.fg. Copy the files ducks.mp3, birds.mp3, barking.mp3 into the directory severalAtOnce.

Write the program

Write these lines into severalAtOnce.fg:

parallel ||
	playSoundFile('ducks.mp3')
||
	playSoundFile('birds.mp3')
||
	playSoundFile('barking.mp3')

displayNewMessage('The End')

Explanation

The program is made up of two parts:

TwoParts

The first part

parallel ||
	playSoundFile('ducks.mp3')
||
	playSoundFile('birds.mp3')
||
	playSoundFile('barking.mp3')

In this part, all the sounds are played at once. They start at the same time. This first part is called a 'parallel construct'.

Warning

The sounds don't finished at the same time. The 'parallel construct' finishes only when all the sounds are finished 😮.

Tip

Don't worry! You'll see later how to interrupt before all the sounds are finished.

The second part

displayNewMessage('The End')

Note

The second part is executed only when the first part is finished (as usual) and, remember, the first part is finished when all the sounds are finished. So, the 'The End' message is only displayed when all the sounds are finished.

The whole

So when you load severalAtOnce.html, here is the timeline:

timeline2C

A little further experiment

The birds sound lasts 55 seconds. If you replace playSoundFile('birds.mp3') by waitSeconds(55), you obtain the same logic: waiting 55 seconds is like playing a silent sound that lasts 55 seconds.

Then, here is the timeline:

timeline2C_b

To sum up

A "parallel construct" has the form:

parallel ||
	block1
||
	block2
||
	block3

Important

It executes "block1", "block2" and "block3" in parallel (just logical parallelism). That is:

  • 👍 "block1", "block2" and "block3" are started in the same time;
  • 👍 "block1", "block2" and "block3" are executed in the same time;
  • ⚠️ but "block1", "block2" and "block3" aren't finished in the same time;
  • 😮⚠️ the "parallel construct" finishes only when all the blocks are finished.