Although Open Server threads are threads of execution that have their own stack and register environments, they share the resources of the operating system process that is executing the Open Server runtime system.
Here are some multithread programming considerations:
Shared resources, such as global data, file handles, and devices, must be protected.
While you are updating a shared global data item, do not call a routine that could suspend the thread unless you have taken steps to prevent other threads from accessing the data. Otherwise, another thread could be working with inconsistent data.
Watch for program logic that behaves as though it has sole access to a resource. An example is a routine that performs part of a calculation using a value from a global variable, then suspends, allowing other threads to alter the global variable. This can cause serial consistency problems. The calculation may be incorrect before it is even complete.
Avoid static variables in routines that more than one thread can execute.
If a routine alters a static variable, and multiple threads can call the routine, you must ensure that multiple instances of the routine do not conflict. There is a greater probability of inconsistent data if the routine returns a pointer to a static variable, since the contents of the variable can be altered while a thread is suspended. It is safer to use automatic variables, because each thread has a stack of its own. The application should provide memory and copy the result there. When you must use static variables, protect them with the techniques discussed above.
SRV_ATTENTION events can be executed at interrupt level. If SRV_ATTENTION handlers manipulate application structures that are also changed or tested in noninterrupt level code, such as other event handlers or service threads, the results of the change or test are unpredictable. Use attention-level wakeups and sleeps to coordinate between interrupt-level SRV_ATTENTION handlers and non interrupt-level code.