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

  • Home
  • SEARCH
  • 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 8158993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:46:59+00:00 2026-06-06T17:46:59+00:00

I have a worker thread processing a queue of work items. work items might

  • 0

I have a worker thread processing a queue of work items. work items might not be processable right now, so the worker thread might push them back into the queue.

void* workerFunc(void* arg) {
    WorkItem* item = NULL;

    while(true) {
        {
            scoped_lock(&queueMutex);
            while(workerRunning && workQueue.empty())
                pthread_cond_wait(&queueCondition, &queueMutex);
            if(!workerRunning)
                break;

            item = workQueue.front();
            workQueue.pop();
        }

        // process item, may take a while (therefore no lock here),
        // may also be considered unprocessable

        if(unprocessable) {
            scoped_lock(&queueMutex);
            workQueue.push(item);
        }
    }
    return NULL;
}

Now i need to do the following: from time to time, i need to scan through the work queue to remove items that are not needed anymore (from the same thread that enqueues work items). I cannot use the queueMutex for this, because i might miss the item that is currently being processed, so i need a way to pause the whole processing thread at a point where all undone items are actually inside the queue (preferrably right at the top of the while loop).

I thought about a second bool variable (“paused”) in combination with another mutex and conditional variable, but then the special case where the worker is waiting for a signal on the queueCondition has to be handled; actually the pthread_cond_wait() call would have to unlock/lock both mutexes.

I guess there must be a simple solution to this problem, but i can’t seem to be able to come up with it – I hope some of you will be able to help me out.

Thanks a lot in advance.

  • 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-06-06T17:47:00+00:00Added an answer on June 6, 2026 at 5:47 pm

    Basically you need to emulate WinAPI’s WaitForMultipleObjects() call on POSIX. POSIX doesn’t have a single API to wait for all types of events/objects as WinAPI does.

    Use pthread_cond_timedwait and clock_gettime. You can refer to this paper WaitFor API for many implementation details.

    There is some interesting code there (too much to post in the answer, but usable) that can solve your problem.

    P.S. Refer to this question for a discussion: WaitForSingleObject and WaitForMultipleObjects equivalent in linux

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

Sidebar

Related Questions

I have an application that has multiple threads processing work from a todo queue.
I have a class containing a worker thread which receives data from a queue
I have a worker thread in a class that is owned by a ChildView.
I have a worker thread spawned from a GUI (for GUI performance), how do
I have a worker thread that may be active for short bursts of time
Sockets on Linux question I have a worker thread that is blocked on an
I have a background worker thread that is constantly syncing data to/from a remote
Hi all i have a background worker thread and some unmanaged code dlls ,
I have a req where in i have to create a worker thread and
I have a WCF service method that's running in a worker thread I spin

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.