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 8991797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:48:38+00:00 2026-06-15T22:48:38+00:00

I have multiple threads processing multiple files in the background, while the program is

  • 0

I have multiple threads processing multiple files in the background, while the program is idle.
To improve disk throughput, I use critical sections to ensure that no two threads ever use the same disk simultaneously.

The (pseudo-)code looks something like this:

void RunThread(HANDLE fileHandle)
{
    // Acquire CRITICAL_SECTION for disk
    CritSecLock diskLock(GetDiskLock(fileHandle));

    for (...)
    {
        // Do some processing on file
    }
}

Once the user requests a file to be processed, I need to stop all threads — except the one which is processing the requested file. Once the file is processed, then I’d like to resume all the threads again.

Given the fact that SuspendThread is a bad idea, how do I go about stopping all threads except the one that is processing the relevant input?

What kind of threading objects/features would I need — mutexes, semaphores, events, or something else? And how would I use them? (I’m hoping for compatibility with Windows XP.)

  • 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-15T22:48:39+00:00Added an answer on June 15, 2026 at 10:48 pm

    The difficulty here isn’t priority as such, it’s the fact that you want a thread to back out of a lock that it’s holding, to let another thread take it. “Priority” relates to which of a set of runnable threads should be scheduled to run — you want to make a thread runnable that isn’t (because it’s waiting on a lock held by another thread).

    So, you want to implement (as you put it):

    if (ThisThreadNeedsToSuspend()) { ReleaseDiskLock(); WaitForResume(); ReacquireDiskLock(); }
    

    Since you’re (wisely) using a scoped lock I would want to invert the logic:

    while (file_is_not_finished) {
        WaitUntilThisThreadCanContinue();
        CritSecLock diskLock(blah);
        process_part_of_the_file();
    }
    ReleasePriority();
    
    ...
    
    void WaitUntilThisThreadCanContinue() {
        MutexLock lock(thread_priority_mutex);
        while (thread_with_priority != NOTHREAD and thread_with_priority != thisthread) {
            condition_variable_wait(thread_priority_condvar);
        }
    }
    
    void GiveAThreadThePriority(threadid) {
        MutexLock lock(thread_priority_mutex);
        thread_with_priority = threadid;
        condition_variable_broadcast(thread_priority_condvar);
    }
    
    void ReleasePriority() {
        MutexLock lock(thread_priority_mutex);
        if (thread_with_priority == thisthread) {
            thread_with_priority = NOTHREAD;
            condition_variable_broadcast(thread_priority_condvar);
        }
    }
    

    Read up on condition variables — all recent OSes have them, with similar basic operations. They’re also in Boost and in C++11.

    If it’s not possible for you to write a function process_part_of_the_file then you can’t structure it this way. Instead you need a scoped lock that can release and regain the disklock. The easiest way to do that is to make it a mutex, then you can wait on a condvar using that same mutex. You can still use the mutex/condvar pair and the thread_with_priority object in much the same way.

    You choose the size of “part of the file” according to how responsive you need the system to be to a change in priority. If you need it to be extremely responsive then the scheme doesn’t really work — this is co-operative multitasking.

    I’m not entirely happy with this answer, the thread with priority can be starved for a long time if there are a lot of other threads that are already waiting on the same disk lock. I’d put in more thought to avoid that. Possibly there should not be a per-disk lock, rather the whole thing should be handled under the condition variable and its associated mutex. I hope this gets you started, though.

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

Sidebar

Related Questions

I have multiple threads that share use of a semaphore. Thread A holds the
I have an application which runs multiple threads. I use MadExcept to catch errors
I have a program that works with multiple threads. For each I set uncaught
I have an application that has multiple threads processing work from a todo queue.
I have multiple threads processing events. I want to assign a nanosecond timestamp to
I have a problem where I need to synchronize processing for multiple threads across
I have multiple threads that are processing rows from the same table that is
I have a need to create multiple processing threads in a new application. Each
I have multiple threads who all need to write to the same Dictionary. I
I have multiple threads each one with its own private concurrent queue and all

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.