contents   index   previous   next



Fibers and Threads

 

Each SEContext can be used by only a single thread at one time. If you want to run multiple scripts simultaneously in a multithreaded application, you need to create one SEContext using seCreateContext per thread. You can in fact create more than one SEContext per thread if you like.

 

Each context contains a copy of much of the same data. Namely, each context will initialize the standard function libraries into its global object in order to allow its scripts to see them. In addition, each context keeps a pool of various kinds of memory available in order to increase performance. As a result, each context has significant overhead of memory. Fibers exist to help alleviate this problem.

 

Fibers are like sibling contexts. Each fiber in the same group has access to the same variables, uses the same pools of memory, and so forth. Therefore the overhead described exists only once even when a large number of fibers exist in the same group. However, fibers are not a replacement for separate contexts in multiple threads. All fibers in the same group are considered one context, so they can only be used by a single thread and only one fiber can be active at once. You can use fibers to cooperatively multitask scripts but since only one fiber can be run at once, fibers do not take advantage of multiple processors. If your machine has multiple processors, and you would like to run multiple scripts taking advantage of all the processors, you must use full contexts not fibers.

 

Fibers are created using the ScriptEase seCreateFiber API call. You pass as a parameter an existing context. The new fiber is created as part of the same fiber group the existing context is part of. You create the first context using seCreateContext then create any number of fiber siblings using seCreateFiber. When you are done, you have a number of related contexts, each which can run its own script. However, all of the contexts share a single global object. You can change the global object in any of the fibers, but the intent of the fibers is to conserve memory so sharing the global object is the norm.

 


USING SE.START

YIELDING AND SUSPENDING

OTHER CONSIDERATIONS