Example #1
0
int main(int argc, char* argv[]) {

    // Create Worker Pool
    initWorkers() ;
    sem_t workPool ;
    sem_init(&workPool, 0, THREAD_WORKERS) ;

    // Create the Socket
    int sockfd, set = 1 ;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        std::cout << "Unable to create a socket!" << std::endl ;
        return 1 ;
    }
    setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set)) ;

    // Initialize the socket structure
    struct sockaddr_in address ;
    bzero(&address, sizeof(address)) ;
    address.sin_family      = AF_INET ;
    address.sin_addr.s_addr = INADDR_ANY ;
    address.sin_port        = htons(SERVER_PORT) ; 

    // Bind socket address to socket
    if (bind(sockfd, (struct sockaddr *) &address, sizeof(address)) == -1) {
        std::cout << "Cannot bind to the socket!" << std::endl ;
        return 1 ;
    }

    // Convert the socket to a listening socket
    if (listen(sockfd, CONNECT_QUEUE) == -1) {
        std::cout << "Unable to make the socket a listening socket!" << std::endl ;
        return 1 ;
    }

    // Loop indefintely to accept any connections
    while (true) {
        int conn ;
        struct sockaddr_in client ;
        socklen_t clientLen = sizeof(client) ;
        bzero(&client, clientLen) ;
        if ((conn = accept(sockfd, (struct sockaddr *) &client, &clientLen)) == -1) {
            std::cout << "Unable to accept any connections!" << std::endl ;
            return 1 ;
     	}
        //fcntl (conn, F_SETFL, fcntl(conn, F_GETFL) | O_NONBLOCK) ;  

        sem_wait (&workPool) ;
        pthread_t thread = getWorker () ;
        pthread_create (&thread, NULL, threadHelper, &conn) ;
        sem_post (&workPool) ;
    }

    // Should never get here
    sem_destroy(&workPool) ;    
    pthread_exit(NULL) ;
    return 1 ;
}
Example #2
0
    void ThreadPool::enqueue(const vector<Task*>& tasks) {
        if(mCoreNum == 1) {
            for(size_t i = 0; i < tasks.size(); ++i) {
                tasks[i]->run();
            }
            return;
        }

        if(mWorkers.size() == 0) {
            initWorkers();
        }
    
        {
            boost::lock_guard<boost::mutex> lk(mTaskQueueMutex);
            for(size_t i = 0; i < tasks.size(); ++i) {
                mTasks.push_back(tasks[i]);
            }
            mTasksNum += mTasks.size();
        }
    };