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 ?
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(andstd::threadtoo, so that can help eliminate some Qt-specific threading code).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::mutexor Qt-specific mutex wrapper. The key thing is that you need some synchronization primitive associated with the shared resource.