Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / C++

Queue Manager: Policy Based Design

Rate me:
Please Sign up or sign in to vote.
4.80/5 (31 votes)
21 Feb 2005CPOL7 min read 99.6K   1.4K   51  
Queue Manager class which automatically adjust the number of threads based on pending tasks in the queue. Policy based design concept is used to provide flexibility and extensibility.
#include <iostream>
#include "QueueMgr.h"
#include <cstdio>
#include <string>

// My Task:
// It has a print method which just prints the task
class MyTask
{
    char m_sString[100];
    public:
     
     MyTask(const char* psString)
     {
         strcpy(m_sString,psString); 
     }
     void Print()
     {
         std::cout << "My Task:" << m_sString <<  std::endl;
         
     }
 };
 
 
// Execution Policy
// Here dummy task sleeps for 100 mill sec and print the object
template<class T>
class MyProcessing
{
    public:
        static void Process(T &obj) throw(std::exception) {
            usleep(15000);
            obj.Print() ;
        }
};

// My custom load balancing criteria
class MyLoadBalanceCriteria
 {
    
     public:
         
         // Define your criteria about load balancing
         // Return value of this function:
         // 1: Create new thread and add to thread pool
         // -1: Remove the thread from the pool
         // 0 : No change in thread pool
         static int LoadBalance(const unsigned nPendingTask, 
                                const unsigned nCurrentNumThreads)                      
         {
             if ( ((float)nPendingTask/nCurrentNumThreads) > 3.0) {
               return 1;
             }
             else if (((float)nPendingTask/nCurrentNumThreads) < 0.25 ) {
               return -1;
             }
             else return 0;
             
         }
};
int main()
{
    // Joshi is a namespace
    
    try {
        Joshi::QueueMgr<MyTask, MyProcessing<MyTask>, MyLoadBalanceCriteria > oMgr;
        oMgr.Init(1);
        for(;;) 
        {
            MyTask oTask("Dummy task");
            oMgr.Add(oTask);
            usleep(1);
        
        }
    }
    catch(std::exception &oException)
    {
        std::cout << oException.what() << std::endl;
    }
    catch(...)
    {
        std::cout << "Unhandled exception" << std::endl;
    }
     return 0; 
    
}



//g++ -g -DDEBUG -c main.cpp -o main.o -I. -I$ACE_INC; g++ -o main main.o -L $ACE_LIB -lACE; main

    
    


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
Rohit Joshi is a software engineer working for a telecom company in USA. He has development expirience using C, C++ ,C#, VoiceXML, ASR, IMAP, LDAP, HTTP, SIP, H323 on unix/linux and platforms.

Comments and Discussions