View previous topic :: View next topic |
Author |
Message |
sean
Joined: 24 Jun 2004 Posts: 609 Location: Bay Area, CA
|
Posted: Fri Mar 31, 2006 6:08 pm Post subject: |
|
|
Oh I see. Let me think about it a bit--it may make more sense to add a removeComplete method instead. In the interim, writing the code by hand is still pretty simple:
Code: | ThreadGroup g = new ThreadGroup;
...
foreach( Thread t; g ) {
if( !t.isRunning )
g.remove( t );
} |
|
|
Back to top |
|
|
lightoze
Joined: 12 Feb 2006 Posts: 35
|
Posted: Fri Mar 31, 2006 6:52 pm Post subject: |
|
|
This would be applicable way too, but it might be better if you remain small additional functionality. Perhaps it will be used a lot in the future by somewho.
P.S. In my opinion, "joinComplete" is better title than "joinFinished". Also in most cases preserve will be false, so this should be default value. |
|
Back to top |
|
|
lightoze
Joined: 12 Feb 2006 Posts: 35
|
Posted: Sat Apr 01, 2006 11:13 am Post subject: |
|
|
Ah, I do not understand anything... I just add void function to ThreadGroup and call it, and get segmentation fault... May be you've done some accidental changes in makefiles? |
|
Back to top |
|
|
sean
Joined: 24 Jun 2004 Posts: 609 Location: Bay Area, CA
|
Posted: Sun Apr 02, 2006 2:53 am Post subject: |
|
|
None of the makefile changes should affect std.thread. Have you gotten the latest of all files from SVN? |
|
Back to top |
|
|
lightoze
Joined: 12 Feb 2006 Posts: 35
|
Posted: Sun Apr 02, 2006 8:44 am Post subject: |
|
|
About errors:
I have managed this out - there was copy of whole sources in "_" dir, located in "src". So "find . -name "*.o" | xargs ar -r libphobos.a" command maked library with double definitions.
About ThreadGroup:
I have mixed up remove method of ThreadGroup with deleting object.
If I create several threads in one group, and then call joinAll or joinCompleted, thread-specific data will be cleared, but two Thread classes will remain in memory. I propose something like this:
Code: | foreach( Thread t; m_all )
{
t.join();
remove( t );
if( !preserve )
delete t;
}
| or this: Code: | foreach( Thread t; m_all )
{
t.join();
if( !preserve )
{
remove( t );
delete t;
}
}
|
ThreadGroup is not thread pool, so in most cases each thread will be used only once.
Currently I use my own class, and I hope sometime this functionality will be in Ares: Code: | class ThreadTracker {
public:
this() {
finish = false;
tracker = new Thread( &this.track );
tracker.start();
}
~this() {
finish = true;
tracker.join();
delete tracker;
}
Thread create( void function() fn ) {
synchronized ( this ) {
Thread t = new Thread( fn );
t.start();
list[t] = t;
return t;
}
}
Thread create( void delegate() dg ) {
synchronized ( this ) {
Thread t = new Thread( dg );
t.start();
list[t] = t;
return t;
}
}
void joinAll() {
synchronized ( this ) {
foreach ( Thread t; list ) {
t.join();
list.remove( t );
delete t;
}
}
}
private:
bool finish;
Thread tracker;
Thread[Thread] list;
void track() {
while ( true ) {
printf( "tick\n" );
synchronized ( this ) {
foreach ( Thread t; list ) {
if ( !t.isRunning ) {
printf( "joined\n" );
t.join();
list.remove( t );
delete t;
}
}
}
if ( finish ) {
return;
}
Thread.sleep( 5000 );
}
}
}
|
|
|
Back to top |
|
|
sean
Joined: 24 Jun 2004 Posts: 609 Location: Bay Area, CA
|
Posted: Sun Apr 02, 2006 1:48 pm Post subject: |
|
|
lightoze wrote: | About ThreadGroup:
I have mixed up remove method of ThreadGroup with deleting object.
If I create several threads in one group, and then call joinAll or joinCompleted, thread-specific data will be cleared, but two Thread classes will remain in memory. I propose something like this:
Code: | foreach( Thread t; m_all )
{
t.join();
remove( t );
if( !preserve )
delete t;
}
| or this: Code: | foreach( Thread t; m_all )
{
t.join();
if( !preserve )
{
remove( t );
delete t;
}
}
|
ThreadGroup is not thread pool, so in most cases each thread will be used only once. |
It is illegal to explicitly delete Thread objects. This is because they are managed internally for use by the GC and this facility must have control over the lifetime of the objects. Removing them from the ThreadGroup simply ensures that there are no references to the Thread objects so they can be cleaned up when this facility is done with them. Generally, that will occur during the next GC collection.
Quote: | Currently I use my own class, and I hope sometime this functionality will be in Ares |
Thanks, I'll give it some thought. |
|
Back to top |
|
|
|