Writing Multithreading RFC Server Applications 
You can use the class library to develop multithreading RFC clients and servers with a minimum of multithreading knowledge. The C++ class library allows you to develop multithreading RFC servers in almost the same way as you develop normal RFC servers; the library encapsulates all the details so you do not have to worry about them.
To write an RFC server with the class library, you must first derive a new class from
CRfcServerFunc (CRfcServerFunc is an abstract class). You must also define the member function Process() in the derived class.If you want to develop a multithreading RFC server using the class library, you must define a new member function,
clone(), in the derived class. Usually, it is easier to implement a copy constructor in the derived class, and then define the clone() function. Unlike Process(), the clone() member function is not a pure virtual function. This means that if you do not implement this function in the derived class, your program will still compile and run. If your application does not use multithreading, your application will run correctly without this function. However, if your application IS multithreading, and you do NOT implement a clone() function, you will receive a run-time error.After you implement the clone() function for the CRfcServerFunc derived class, a CRfc server application performs almost like a non-multithreading RFC server program. The only exception is when an application calls the CRfcServerApp class
Run() function. In this case, you need to define the following argument:Run(int nThreads=0)
The previous definition of this function had no arguments. If the application does not pass an argument, or if the application passes an argument with a value of zero or less to the function, the CRfc server program does not multithread the application. In this case, you do not need to implement a clone() function.
The parameter nThreads is the number of threads you want the class library to create for this server application. Currently, you can use up to 1000 threads with the library.
Determining the Number of Threads
How many threads does your application need? There is no firm rule, so use your experience to guide you. Below are some tips to help you select the number of threads:
Case 1: The server has only one CPU and very few clients.
It is very unlikely that more than one client will call this server simultaneously. If so, then do NOT use multithreading. You can call Run without an argument, or use zero as the argument. You do not need to implement the clone() function in the derived CRfcServerFunc class.
Case 2: The server has more than one CPU and many clients.
Usually, there will be only one client at a time calling this server. Do NOT use multithreading.
Case 3: The server has more than one CPU.
There might be more than one client calling the server simultaneously. However, the number of clients is usually smaller than the number of CPUs. Set the number of child threads equal to the number of CPUs.
Case 4: The server has one or more CPUs.
Usually the number of clients calling the server is greater than the number of CPUs. In this instance, the number of child threads needs to be selected experimentally. Your client response and total processing time are the determining factors.
If you want to monitor which threads are performing which tasks at what times, you can access the global variable,
rfc_thread[10000] , exposed by the class library (extern int rfc_thread[10000];) . This array holds the IDs of threads created by the class library. The array index is the RFC_HANDLE for the RFC connection.For example, using the derived class Process() member function, (this function must be derived because it is defined as a virtual function in CRfcServerFunc), a user can display the activities of each thread on the console or a GUI. To do this, get the RFC_HANDLE, by using the following syntax:
(RFC_HANDLE RfcHandle; GET_VERIFIED_CONNECTION(RfcHandle);)
You can then use RFC_HANDLE RfcHandle to access the ID
(rfc_thread[RfcHandle]) of the current running thread. For details, please see the sample programs.To conclude, if you have already developed an Rfc server with the class library, it is easy to implement multithreading. You only need to use the clone() function and a copy constructor derived from the CRfcServerFunc, and enter the number of threads as the argument when calling
Run(int nThreads=0) . For more help on developing and testing a multithreaded server with the class library, please see the ThreadServer.h and ThreadServer.cpp programming examples.