Threading model of SmartFoxServer 2X for Server side Extensions

March 10th, 2011 by aabhaanjan

SmartFoxServer 2X runs all Extensions in a multithreaded environment. In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code. Because a program can contain more than one thread, a single program can use multiple threads to perform two or more tasks at once.

Multithreading is important for these main reasons :

  1. Multithreading enables to write very efficient programs because it lets utilize the idle time that is present in most programs. Most I/O devices, whether they be network ports, disk drives, or the keyboard, are much slower than the CPU. Thus, a program will often spend a majority of its execution time waiting to send or receive information to or from a device. Multithreading program can execute another task during this idle time.
  2. While one part of a program is sending a block of data to the server/client still another can be buffering the next block of data to send.
  3. Multithreading is relates to Java’s eventhandling model. A program must respond quickly to an event and then return. An event handler must not retain control of the CPU for an extended period of time. If it does, other events will not be handled in a proper manner. This will make an application appear Slow-moving or inactive. It is also possible that an event will be missed. Therefore, if an event requires some extended action, then it must be performed by a separate thread.

Because of above mentioned  reason there are fundamentally two separate thread pools operating on an Extension of SFS2X:

a).  the ExtensionController (Which Handles Client Requests)

b) . the EventManager. (Which  Dispatches the server events)

Synchronization of Shared State

A thread can be in one of several states. It can be running. It can be ready to run as soon as it gets CPU time. A running thread can be suspended, which is a temporary halt to its execution. It can later be resumed. A thread can be blocked when waiting for a resource. A thread can be terminated, in which case its execution ends and cannot be resumed.Since in the case of SFS2X threads operate concurrently on the Extension code access of shared state should be properly synchronized. Although The SFS2X API already take care of of these points.

Tags: , , , , , , , , , , , ,