thanks Remy.
However, it was wanted to start a certain number of threads through a for () loop and then, when one of them ends, a new thread starts in its place.
Thanks again
Multi thread, array yes or no?
Moderator: 2ffat
Re: Multi thread, array yes or no?
The example I gave you on Feb 5th does exactly that.mark_c wrote:However, it was wanted to start a certain number of threads through a for () loop and then, when one of them ends, a new thread starts in its place.
Remy Lebeau (TeamB)
Lebeau Software
Lebeau Software
Re: Multi thread, array yes or no?
sorry Remy,rlebeau wrote:The example I gave you on Feb 5th does exactly that.mark_c wrote:However, it was wanted to start a certain number of threads through a for () loop and then, when one of them ends, a new thread starts in its place.
I had missed this sentence of yours, which I then wrote too, a forgetfulness, my fault.Once a thread terminates, it can't be reused.
Re: Multi thread, array yes or no?
a doubt with the threads:
when a thread ends if I use FreeOnTerminate = true; it still makes sense to use:
when do you want to instantiate the same thread again?
I think not because, if I use FreeOnTerminate = true; and then I call delete MyThread you throw an exception.
when a thread ends if I use FreeOnTerminate = true; it still makes sense to use:
Code: Select all
MyThread-> Terminate ();
MyThread-> WaitFor ();
delete MyThread;
I think not because, if I use FreeOnTerminate = true; and then I call delete MyThread you throw an exception.
Re: Multi thread, array yes or no?
No, it does not make sense, because:mark_c wrote:when a thread ends if I use FreeOnTerminate = true; it still makes sense to use:
Code: Select all
MyThread-> Terminate (); MyThread-> WaitFor (); delete MyThread;
- it is simply wrong to call 'delete' on a self-freeing thread. That will cause a double-delete.
- if you call WaitFor() on a self-freeing thread, WaitFor() will almost always crash, either with an Access Violation, or an EThread or EOSError exception (though technically, this is undefined behavior, so a crash is not guaranteed). This is because of a race condition where WaitFor() uses its 'this' pointer to access the thread's Handle property, but 'this' can be freed at any time before, or while, WaitFor() is running. And since WaitFor() is meant to wait for the thread to fully terminate, 'this' is pretty much guaranteed to be invalid by the time WaitFor() exits. So, you can't safely use WaitFor() and FreeOnTerminate together.
You can do that in the OnTerminate event, like I showed you earlier.mark_c wrote:when do you want to instantiate the same thread again?
You are likely to get an exception from WaitFor() before you even reach the 'delete'. But even if no exception occurs on WaitFor(), you are likely to get one from 'delete', but that is not guaranteed, either. Deleting an object twice is undefined behavior.mark_c wrote:I think not because, if I use FreeOnTerminate = true; and then I call delete MyThread you throw an exception.
Last edited by rlebeau on Wed Apr 01, 2020 6:48 pm, edited 4 times in total.
Remy Lebeau (TeamB)
Lebeau Software
Lebeau Software
Re: Multi thread, array yes or no?
but I can do that, right?
FreeOnTerminate = false;
MyThread-> Terminate ();
MyThread-> WaitFor ();
delete MyThread;
and so I have no more exceptions
FreeOnTerminate = false;
MyThread-> Terminate ();
MyThread-> WaitFor ();
delete MyThread;
and so I have no more exceptions
Re: Multi thread, array yes or no?
If you use FreeOnTerminate=false then it is safe to use WaitFor() and 'delete' (in fact, that is pretty much a requirement).
However, you must make sure that FreeOnTerminate is false (which it is by default) BEFORE the thread's Execute() method exits. Any subsequent changes to FreeOnTerminate after that point DO NOT take effect, as the FreeOnTerminate value is captured immediately upon Execute()'s exit, before DoTerminate() is called to fire the OnTerminate event.
However, you must make sure that FreeOnTerminate is false (which it is by default) BEFORE the thread's Execute() method exits. Any subsequent changes to FreeOnTerminate after that point DO NOT take effect, as the FreeOnTerminate value is captured immediately upon Execute()'s exit, before DoTerminate() is called to fire the OnTerminate event.
Remy Lebeau (TeamB)
Lebeau Software
Lebeau Software