Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6040271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:29:23+00:00 2026-05-23T06:29:23+00:00

I am working on a network programming and I have created a thread pool.

  • 0

I am working on a network programming and I have created a thread pool. It basically has a queue with mutex lock and condition variable and 5 child threads compete to get a work from the queue. It seems like working correctly with locking and unlocking with the condition variable.

But the problems is that if I call a function from the child thread then only one thread is allowed work on the function(fromByte). For example, if thread 1 called the function then the other thread won’t be able to enter the function.

void WorkHandler::workLoop(){
    printf("WorkHandler::workLoop, called\n");

    while(m_workHandlerRun){
        Work *work = getWork();
        char *pdata = work->getMsg();
        /*
         * Get type only
         */
        unsigned char type = pdata[0];

        printf("WorkHandler::workLoop, type %d\n", type);

        Packet *packet = m_packetFactory->createInstance(static_cast<PACKET_TYPES>(type));
        packet->fromByte(pdata);
    }
}

This is the work loop that the child threads are running and it calls the fromByte() after it gets the appropriate class instance from the factory. If I see the log statement then only one thread are allowed to work on the fromByte function and if the thread finished then other thread can work in the function. In other words, if a thread is currently in the function then other threads wait until the thread finish the work.

bool WorkHandler::initThreads(){

    for(int i=0; i < m_maxThreads; i++){
        pthread_t *thread(new pthread_t);
        m_workThreadList.push_back(thread);

        if(pthread_create(thread, NULL, runWorkThread, reinterpret_cast<void *>(this))!=0){
            perror("WorkHandler::initThreads, pthread_create error \n");
            return false;
        }

        pthread_detach(*thread);
    }

    return true;
}

This is how I spawn a thread and runWorkThread is a static method to call the workLoop function. How do I fix my code so that child threads can work on the function concurrently. Thanks in advance..

Edit

I am locking and unlocking like this

void WorkHandler::addWork(Work* w){
    printf("WorkHandler::insertWork Thread, insertWork locking \n");
    lock();
    printf("WorkHandler::insertWork Locked, and inserting into queue \n");
    m_workQueue.push(w);
    signal();
    unLock();
}

Work* WorkHandler::getWork(){
    printf("WorkHandler::getWork, locking (tid : %lu) \n", pthread_self());
    lock();
    printf("WorkHandler::getWork, locked (tid : %lu) \n", pthread_self());
    while(m_workQueue.empty()){//Need 'while' instead of 'If'
        printf("WorkHandler::getWork, waiting... (tid : %lu) \n", pthread_self());
        wait();
        printf("WorkHandler::getWork, waiting DONE (tid : %lu) \n", pthread_self());
    }
    Work *work = m_workQueue.front();
    printf("WorkHandler::getWork, got a job (tid : %lu) \n", pthread_self());
    m_workQueue.pop();
    unLock();

    return work;
}

Also, this class extends MutexdCondtion class I created
MutexCondition.cpp file

bool MutexCondition::init(){
    printf("MutexCondition::init called\n");
    pthread_mutex_init(&m_mut, NULL);
    pthread_cond_init(&m_con, NULL);
    return true;
}

bool MutexCondition::destroy(){
    pthread_mutex_destroy(&m_mut);
    pthread_cond_destroy(&m_con);
    return true;
}

bool MutexCondition::lock(){
    pthread_mutex_lock(&m_mut);
    return true;
}

bool MutexCondition::unLock(){
    pthread_mutex_unlock(&m_mut);
    return true;
}

bool MutexCondition::wait(){
    pthread_cond_wait(&m_con, &m_mut);
    return true;
}

bool MutexCondition::signal(){
    pthread_cond_signal(&m_con);
    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:29:24+00:00Added an answer on May 23, 2026 at 6:29 am

    the problem is that if I call a
    function from the child thread then
    only one thread is allowed work

    If you base this assumption on your printf, then you are wrong. It’s possible that working thread finishes it’s work before new item is placed in queue. This creates possibility, that same function will pick up two items in a row.

    There is no way that for other threads to wait for working one, since there is nothing that blocks them after getWork() return.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I'm working on some network programming in C, and it would seem that
i'm working on a network programming assignment about writing a simple IM system (pretty
I have an idea of a social network website and I will be working
I am working on a social network project in PHP/MySQL. If you have ever
My question is one I have pondered around when working on a demanding network
I have a network application working on Linux. What I want to do is
I am currently working on network software. It has one main class, server which
I am working on a social network type project, as most social networks have,
I'm working on a .net solution that is run completely inside a single network.
I'm working on a Linux based server system in which there are two network

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.