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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:07:06+00:00 2026-06-13T04:07:06+00:00

i have thread pool that create threads each thread worker calculate some work and

  • 0

i have thread pool that create threads each thread worker calculate some work and when it done
it writes the result to file , there is only 1 result file , that each worker thread needs to write to .
now my question is how i guarantee that there wouldn’t be any locks or missing write data to the file where allot of threads trying to write to single file ? what is the right strategy for such scenario?
mybe keep all result in memory ? or chanks of results

im using already the QThreadPool framework , and i need to find solution with it.
also i wander , does writing to single file from worker threads , i will have to use
singleton file manager or static class , is it good idea? for multithreaded app ?

  • 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-13T04:07:09+00:00Added an answer on June 13, 2026 at 4:07 am

    So you have many concurrent threads competing for one shared resource. That begs a synchronization primitive of some sort, for example a mutex.

    Here’s (non-Qt specific) code showcasing 10 threads simultaneously writing to a single file. On a side note, C++11 introduced a lot of goodies like std::mutex (and std::thread too, so that can help eliminate some Qt-specific threading code).

    #include <fstream>   
    #include <mutex>
    #include <thread>    
    #include <vector>
    
    std::mutex m;
    std::ofstream file;
    
    int main() {
      file.open("file.txt");
    
      std::vector<std::thread> workers;
      for (int i = 0; i < 10; ++i) {       
        workers.push_back(std::thread([=i] {
          for (int j = 0; j < 10; ++j) {
            std::lock_guard<std::mutex> lock(m);
            file << "thread " << i << ": " << j << endl;
          }
        }));
      }
      for (auto& worker : workers) {
        worker.join();
      }
    
      file.close();
      return 0;
    }
    

    Of course, if you have a lot of places in your code accessing the shared resource, it’s better to encapsulate it, along with the mutex, in some “accessor” class that would manage all state-modifying calls to the resource.

    P.S. If you’re not on C++11 compiler, you can use boost::mutex or Qt-specific mutex wrapper. The key thing is that you need some synchronization primitive associated with the shared resource.

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

Sidebar

Related Questions

I have a custom thread pool class, that creates some threads that each wait
I'm using Thread::Pool::Simple to create a few working threads. Each working thread does some
I have some code that makes heavy use of a thread pool, which I
I have a thread from a thread pool in a service that does quite
I would like to create a thread pool. I have a class called ServerThread.cpp,
I have just learned that you can use a Thread pool for multi client
I have a task that would benefit from the Thread Pool design pattern (many
I've created a pool of 4 worker threads to process some files. In the
I have an application that does some image processing on each new frame, recently
I have a thread pool (executor) which I would like to monitor for excessive

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.