13.7 Activating Knowledge Sources Using Pthreads

The knowledge sources are implemented within separate threads. The blackboard's constructor creates the threads and assigns each thread a specific knowledge source. This gives the blackboard its MIMD model. Example 13.11 contains part of the constructor for the blackboard.

Example 13.11 The blackboard's constructor used to create the threads that will contain the knowledge sources.
 blackboard::blackboard(void) {     pthread_t Tid[4];     //...     try{           pthread_create(&Tid[0],NULL,suggestionForMajor,                          NULL);           pthread_create(&Tid[1],NULL,suggestionForMinor,                          NULL);           pthread_create(&Tid[2],NULL,suggestionForGeneral,                          NULL);           pthread_create(&Tid[3],NULL,suggestionForElective,                          NULL);           pthread_join(Tid[0],NULL);           pthread_join(Tid[1],NULL);           pthread_join(Tid[2],NULL);           pthread_join(Tid[3],NULL);     }     //... } 

Notice that the constructor calls the pthread_join() routine. This causes the constructor to wait for these four threads to terminate before it continues. These threads can be activated from other member functions of the blackboard. However, the particular processing these knowledge sources are performing for the constructor is a preliminary kind of initialization for the blackboard, so it is totally appropriate for the blackboard to wait on these threads before it continues to construct the object. This technique of creating threads within the constructor also raises error handling and exception handling issues. What happens if the threads fail for some reason? Since constructors don't have return values, exception handling must be used.

Each thread is associated with a function. In this case:

 void *suggestionForMajor(void *X); void *suggestionForMinor(void *X); void *suggestionForGeneral(void *X); void *suggestionForElective(void *X); 

These four functions are used by the threads to implement the functionality of these particular knowledge sources. Since the blackboard is a global object, each of these functions has immediate access to the blackboard's member functions. So the knowledge sources may call the blackboard's member functions directly, such as:

 //... Combination.generateCombinations(1,9,Courses); Result = Combination.element(9); //... Blackboard.suggestionsForMinor(Value); //... 

Since some divisions of the blackboard are restricted to a particular knowledge source, these divisions of the blackboard may be accessed using CRCW policies, as shown in Figure 13-9.

Figure 13-9. The four knowledge sources may concurrently read and write their section of the blackboard.

graphics/13fig09.gif

The type of parallelism shown in Figure 13-9 is a natural scheme in blackboard systems because the blackboard is often divided into sections, with each section referring to a certain part of the problem or subproblem. There is typically one knowledge source per problem area, so these sections may be accessed concurrently.



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