13.6 Implementing the Blackboard Using Global Objects

The choice of a CORBA-based blackboard is a natural choice when the knowledge sources will be implemented within an intranet or the Internet environment or when the knowledge sources will be implemented in separate processes for purposes of modularity, encapsulation, and so on. However, distributed blackboards are not always necessary. In the case where the knowledge sources can be implemented within the same process and on the same computer, multiple threads provide a superior solution because they will be faster, have less overhead, and are easier to use and set up. The communication between multiple threads is also easier because the threads share the same address space and can use global variables . In fact, with threads the blackboard will be instantiated as a global object available to all of the threads within the process. There is no need for inter-process communication, socket communication, or any other kind of network communication when the knowledge sources are implemented as threads within a single program. Also, the added layer of the CORBA protocol is not necessary and the objects may be designed as regular C++ classes. If the program is running on a machine that has multiple processors, then the threads may run concurrently on as many processors as are available. The thread's configuration of the blackboard is very attractive in SMP and MPP systems. In general, threads will have the best performance. Threads are often referred to as lightweight processes because they don't require the same overhead as traditional UNIX/Linux processes. The POSIX threads (Pthreads) library offers virtually everything needed for knowledge source creation and management. Figure 13-7 contrasts the three basic configurations for process distributions for blackboards and knowledge sources.

Figure 13-7. Contrasts of the three basic configurations for process distribution for blackboards and knowledge sources.

graphics/13fig07.gif

Because the blackboard is implemented within a multithread environment, then Pthread mutexes and condition variables may be used to synchronize access to the blackboard. Of course the mutexes and condition variables should be encapsulated within interface classes, as discussed in Chapter 11. Also, pthread_cond_signal() and pthread_cond_broadcast() can be used to coordinate and synchronize the work that the knowledge sources are performing. Since the blackboard creates the threads it will have easy access to the thread id of each knowledge source. This means the blackboard can cancel a thread if necessary with pthread_cancel() . Also, the blackboard will be able to synchronize on the various knowledge sources using the pthread_join() routine. In addition to the performance and ease-of-use advantages of threads and global blackboards, there is also the issue of error and exception management. In general, it is easier to deal with errors and exceptions within the same process than between different processes, and with errors on the same machine than between different machines. Figure 13-8 shows the exception and error level difficulty when handling errors and difficulties within a program.

Figure 13-8. The exception and error level difficulty.

graphics/13fig08.gif

Since the knowledge sources are implemented within separate threads within the same process, any errors or exceptions that occur will be at Level 2. Whenever programs that require concurrency are designed and developed, the complexity of handling and recovering from errors and exceptions must be considered . The blackboard implemented as a global object and the knowledge sources implemented as threads are the simplest architecture when using the blackboard model for concurrency. Example 13.10 contains an excerpt declaration from our course advisor blackboard.

Example 13.10 An excerpt from the course advisor blackboard designed for a threaded environment.
 class blackboard{ protected:    //...    set<long> SuggestionForMajor;    set<long> SuggestionForMinor;    set<long> SuggestionForGeneral;    set<long> SuggestionForElective;    set<long> Schedule;    set<long> DegreePlan;    mutex Mutex[10];    //... public:    blackboard(void);   ~blackboard(void);    void suggestionsForMajor(set<long> &X);    void suggestionsForMinor(set<long> &X);    void suggestionsForGeneral(set<long> &X);    void suggestionsForElectives(set<long> &X);    set<long> currentDegreePlan(void);    set<long> suggestedSchedule(void);    //... }; 

This blackboard class is designed to be instantiated as a global object accessible to all the threads within a program. Notice that the blackboard class in Example 13.10 has an array of mutexes. These mutexes will be used to protect the critical sections within the blackboard. The knowledge sources are virtually unaware of the synchronized access to the critical sections because the synchronization is encapsulated within the blackboard.



Parallel and Distributed Programming Using C++
Parallel and Distributed Programming Using C++
ISBN: 0131013769
EAN: 2147483647
Year: 2002
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net