The script engine can run multiple scripts in parallel, i.e. concurrent scripts. This can be done easily using multithreading where each thread runs its own script context, but this article is going to explain how it is done without multithreading.
In order to execute multiple scripts in parallel, each script must have it's own asIScriptContext, where the context is set up as for a normal function call. Then the application needs to set a timeout for each context. When the timeout is reached, the context should be suspended, so that the next context can execute for a while.
Here's a very simple example of how this can be done:
The application can manage the execution of the scripts in a simple round-robin fashion, where each script gets equal amount of execution time, or a more intricate management algorithm can be built, e.g. to give more execution time to high priority scripts etc.
The time out function, i.e. SetTimeoutForContext in the example above, can be implemented in two different ways. Through the use of line callbacks, where the context will invoke the callback for each statement in the script. The callback can then check if the timeout limit has been reached and suspend the context.
The other way is through the use of a timeout thread, where the thread simply sleeps until the timeout limit has been reached, and when the thread wakes up it suspends the context (if it is still running).
The timeout thread is probably the easiest to implement, and also doesn't impact the performance as much as the line callback. The line callback may still have to be used if the target OS doesn't support multithreading though.
Here's a simple example of how to implement the timeout function with a separate thread:
Observe that the routines for multithreading usually differ a lot depending on the target system. The above code is for Windows and will most likely require adaption to work on any other system.