Non-Reentrant Class Use In Multithreaded Program
-
But I need to be able to stop the worker thread from the gui thread, not just when the program is scanning files but also when it is copying files, using the same cancel condition as created by clicking the cancel button in the gui thread. All the logic is handled in the worker thread both scanning, volume shadow copy service, file copying and everything. All of these operations need to be cancelled if desired. The worker thread remains alive it just gets reused for each backup that is performed. I did just find a workaround as per your suggestion got my creative juices flowing... I can check the std::atomic_bool cancel variable in the CopyFIleEx progress callback function and if it is true then return PROGRESS_CANCEL. Can you think of another way to do this though? Just curious.
@Crag_Hack said in Non-Reentrant Class Use In Multithreaded Program:
I can check the std::atomic_bool cancel variable in the CopyFIleEx progress callback function and if it is true then return PROGRESS_CANCEL. Can you think of another way to do this though?
This sounds like a decent plan to me.
-
Will this code maintain the atomicity of the cancel variable? And should I declare cancel volatile? I'm guessing std:atomic_bool will make that unncecessary...
class Worker : public QObject { bool getStop() { return cancel; } volatile std::atomic_bool cancel; ... DWORD Worker::copyProgress(LARGE_INTEGER totalSize, LARGE_INTEGER totalTransferred, LARGE_INTEGER, LARGE_INTEGER, DWORD, DWORD, HANDLE, HANDLE, LPVOID data) { Worker *worker = static_cast<Worker *>(data); if (worker->getStop()) return PROGRESS_CANCEL;
-
Will this code maintain the atomicity of the cancel variable? And should I declare cancel volatile? I'm guessing std:atomic_bool will make that unncecessary...
class Worker : public QObject { bool getStop() { return cancel; } volatile std::atomic_bool cancel; ... DWORD Worker::copyProgress(LARGE_INTEGER totalSize, LARGE_INTEGER totalTransferred, LARGE_INTEGER, LARGE_INTEGER, DWORD, DWORD, HANDLE, HANDLE, LPVOID data) { Worker *worker = static_cast<Worker *>(data); if (worker->getStop()) return PROGRESS_CANCEL;
@Crag_Hack said in Non-Reentrant Class Use In Multithreaded Program:
Will this code maintain the atomicity of the cancel variable?
Yes, of course.
And should I declare cancel volatile?
No, no reason to do that. Forget volatile, especially if you're not intimately familiar with what it does. It's very special and here there is no a proper reason to use it.
I'm guessing std:atomic_bool will make that unncecessary...
That is correct.
volatile
means something quite different anyway, it doesn't provide atomicity at all.