SYS-CON MEDIA Authors: Kevin Remde, Lori MacVittie, Peter Silva

Related Topics: PowerBuilder

PowerBuilder: Article

Bind Thread and Other Component Properties

Bind Thread and Other Component Properties

When creating an object that's destined to become a component in EAServer, you have a myriad of choices. Many of them surround how the object is built, the methods that are created, how transactions are handled, and much more.

Those items, however, are the topics of other articles. In this article I focus on component properties. Specifically, I focus on those properties that relate to the way component threads are created, used, and destroyed in EAServer.

When researching this article, the first place I visited was the Jaguar documentation, which contains the following paragraph:

Bind Thread: When this option is enabled, component instances are bound to the creating thread. Enable this option if the component uses thread-local storage. For ActiveX components, this option must be enabled. For other component types, enable the option only if you are sure that your component uses thread-local storage.

If the Bind Thread option is selected, multiple instances may still run concurrently on separate threads. To ensure that only one instance is active at a time, make sure that the Concurrency option is not selected.

When Bind Thread is enabled, instances are pooled if the Pooling option is enabled. The thread is pooled with the instance in this case.

Further research yielded variations of this same paragraph, which led to the following questions:

  1. What is a thread?
  2. What is thread-local storage?
  3. What other properties are related and how?
Knowing the answers to each of these questions will help you build better, faster, and more powerful components. Let's start at the beginning.

What Is a Thread?
Windows NT documentation defines a thread as "the most basic operating system unit capable of executing instructions on the CPU. A thread represents an execution path and has its own address space and its own programming stack. Each process has at least one thread - the primary or main thread - which is started automatically within the application."

The "process" referred to in the thread definition is an application or program. In a 32-bit Windows operating system, each process is independent of any other process running on that machine. Processes are given their own memory-addressing space and use a separate program stack. This protects one process from writing over the memory of another, preventing an errant application from bringing down the entire system.

A typical program will have a single execution path along the primary thread. It's in the primary thread that the main portion of the program is executed. However, a process can have several threads executing within it and, therefore, have several execution paths. An application that has more than one thread is called a multithreaded application.

EAServer is such an application. Each time a component is instantiated, a new thread is "spawned" or created to run that component. This not only allows multiple components to be running in memory simultaneously, but it also ensures that EAServer won't crash because of a bad component. When you "bind" the thread of a component to its client, you ensure that one instance of the component has a single execution path that's tied to the client. Multiple instances can be invoked, each with its own thread.

With components that use thread-local storage, Bind Thread must be set.

What Is Thread - Local Storage?
Remember that a thread has its own execution path, address space, and programming stack. In other words, a thread can house its own data. When processes depend on the data contained in the thread, they're said to use thread-local storage. A component will also use thread-local storage if it utilizes resources that employ it.

DataStores use internal Windows resources that use thread-local storage, requiring the component that utilizes them to use it too. This is true only if the DataStore is an instance variable that's not destroyed between method calls. Microsoft COM (Component Object Model) components (such as ActiveX components) also require thread-local storage. Note that thread-local storage is specific to Microsoft Windows machines. Any component that uses it must have the Bind Thread property set.

How do you know if your component uses thread-local storage? Does it use Windows resources and maintain those resources between method calls (as an instance DataStore does)? Is it a COM component that's tied to Windows? Are you accessing and maintaining Windows data (such as a Windows handle)? If you answered "yes" to any of these questions, then your component uses thread-local storage and requires Bind Thread to be set to true.

What Other Properties Are Related and How?
As mentioned earlier, when Pooling and Bind Thread are both set, the thread is pooled with the component. The fact that Bind Thread is set doesn't affect performance any more than Bind Thread alone does.

Bind Thread combined with concurrency allows multiple instances of a bound component to be created. Because the threads are bound to their clients, combining concurrency with Bind Thread reduces resources on the server. It's possible for very large applications to instantiate so many threads that all resources are consumed, causing EAServer to crash. Future enhancements of EAServer will prevent crashes under these circumstances by freeing memory and/or forcing waits.

Concurrency determines if clients can simultaneously invoke methods on the same instance. EAServer allows the methods of a single instance to be run in separate threads and thus be accessed by more than one client at a time. To configure this option, you also need to set the sharing option. Table 1 shows the relationship between the sharing and concurrency options.

Although there are several other component properties, none of them deal with threads, even though you could attempt to make a case for stateless components and threads. The issues surrounding stateless components and threads are the same ones I've already discussed in relation to Bind Thread.

Conclusion
For PowerBuilder components, use local DataStores instead of instance DataStores and avoid holding Windows resources between method calls. These two items alone mean you don't need to set the Bind Thread property, giving your application a performance boost. For another performance boost, set concurrency on but leave sharing off. In fact, for PowerBuilder components, never set both concurrency and sharing to on. If you do, EAServer will turn sharing or concurrency off automatically. It will also turn concurrency off if your component uses thread-local storage. To determine if EAServer has done this, check the server log.

Understanding threads will help you build better components. Knowing the properties that relate to threads can help you create components that behave and perform well.

More Stories By Timothy P. Bloodworth

Timothy P. Beck, managing partner and founder of IntelliServe LLC, has been working with PowerBuilder since 1991 and has been a certified instructor of PowerSoft and later Sybase technology for nearly eight years. He has personally assisted dozens of customers in developing Web-ready applications using Sybase's EAServer. IntelliServe. He is a Sybase Consulting Partner, an iAnywhere Systems Integrator, and a Sybase Education Agent.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.