There are 2 threads in my application - one UI, and the second is updating some data. The class of the second thread inherits the wxThread class, and has some variables, in particular, Worker *worker.
class Worker {
public:
virtualvoid work() {}
};
class WorkerThread : public wxThread {
public:
bool isRunning = true;
Worker *worker;
virtual ExitCode Entry() {
while (isRunning) {
if (worker == nullptr) continue;
// I understand that it is unsafe - the pointer
// could already become nullptr,
// but this is done solely for example
worker->work();
usleep(UPDATE_DELAY_USEC);
}
std::cout << "Done\n";
return 0;
}
};
If I change the value of the worker from the first thread to nullptr, the application will continue working fine. If, for example, I change the value to new Worker() or the same value as it was, the application will close. Without any errors printed to the console.
> // I understand that it is unsafe - the pointer
> // could already become nullptr,
> // but this is done solely for example
OK, so how much have you deleted and paraphrased for us just for the sake of a smaller post?
Communication between threads need to be "synchronized" in some way (e.g. by using atomic variables or mutexes). Multithreading is difficult and not something you can just try and see if it works, you need to actually understand what you're doing to make it work correctly, so I think you should spend some serious time reading up on the subject before attempting.
OK, so how much have you deleted and paraphrased for us just for the sake of a smaller post?
Not at all, I even added a condition for checking on nullptr so that the pointer could be set to this value and no error occurred. I wanted to show that if set value to nullptr, then the application will continue to work as intended, and if not nullptr (even just the previous value!), it will close
You are paraphrasing.
Because if you want actual help (and not vague hand-waiving through ad-hoc lists of possible causes), we need actual code we can copy/paste/try for ourselves.
The problem was solved... I was looking for a problem where there is none. You are right, I would have to give the whole code. The problem was that I first created a Notebook, respectively, onPageChanged was called, in which the pointer changed, which belonged to a class that had not yet been created.