Multi thread, array yes or no?

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Re: Multi thread, array yes or no?

Postby mark_c » Fri Feb 07, 2020 12:53 am

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
mark_c
BCBJ Master
BCBJ Master
 
Posts: 234
Joined: Thu Jun 21, 2012 1:13 am

Re: Multi thread, array yes or no?

Postby rlebeau » Fri Feb 07, 2020 7:05 pm

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.


The example I gave you on Feb 5th does exactly that.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1671
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Multi thread, array yes or no?

Postby mark_c » Sat Feb 08, 2020 1:56 am

rlebeau wrote:
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.


The example I gave you on Feb 5th does exactly that.


sorry Remy,
Once a thread terminates, it can't be reused.

I had missed this sentence of yours, which I then wrote too, a forgetfulness, my fault.
mark_c
BCBJ Master
BCBJ Master
 
Posts: 234
Joined: Thu Jun 21, 2012 1:13 am

Re: Multi thread, array yes or no?

Postby mark_c » Wed Apr 01, 2020 9:08 am

a doubt with the threads:
when a thread ends if I use FreeOnTerminate = true; it still makes sense to use:

Code: Select all
MyThread-> Terminate ();
MyThread-> WaitFor ();
delete MyThread;


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.
mark_c
BCBJ Master
BCBJ Master
 
Posts: 234
Joined: Thu Jun 21, 2012 1:13 am

Re: Multi thread, array yes or no?

Postby rlebeau » Wed Apr 01, 2020 11:14 am

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;



No, it does not make sense, because:

- 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.

mark_c wrote:when do you want to instantiate the same thread again?


You can do that in the OnTerminate event, like I showed you earlier.

mark_c wrote:I think not because, if I use FreeOnTerminate = true; and then I call delete MyThread you throw an exception.


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.
Last edited by rlebeau on Wed Apr 01, 2020 6:48 pm, edited 4 times in total.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1671
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Multi thread, array yes or no?

Postby mark_c » Wed Apr 01, 2020 12:47 pm

but I can do that, right?

FreeOnTerminate = false;

MyThread-> Terminate ();
MyThread-> WaitFor ();
delete MyThread;

and so I have no more exceptions
mark_c
BCBJ Master
BCBJ Master
 
Posts: 234
Joined: Thu Jun 21, 2012 1:13 am

Re: Multi thread, array yes or no?

Postby rlebeau » Wed Apr 01, 2020 6:53 pm

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.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1671
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Previous

Return to Technical

Who is online

Users browsing this forum: No registered users and 49 guests

cron