Thread Joinable

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Locked
Queen
In need of some credit
In need of some credit
Posts: 8
Joined: Sun Jan 08, 2006 6:17 pm

Thread Joinable

Post by Queen » Mon Jan 16, 2006 8:32 pm

Hi!

I'm doing a simply program with threads.
I've got two classes MyApp and MyThread. In the last one I implements the thinks that have to do the thread. And in the first one I create two threads of MyThread class. I want to wait for finish MyApp that the two threads have been finished before. I do it with the instruccion thread1->Wait(). But I don't do it in the rigth way.

My code is this:

virtual ExitCode Entry ()
{
Saluda();
for ( m_count = 0; m_count < 10; m_count++ )
{
if ( TestDestroy() ){
break;
printf("\n Thread %hu
wxThread::Sleep(1000);
}

printf("\n Thread ha acabado.\n");


return (ExitCode)1;
}



bool MyApp::OnInit()
{
MyThread *thread1, *thread2;

thread1=new MyThread("\n Numero 1", 1);
thread1->SetPriority(WXTHREAD_DEFAULT_PRIORITY);

thread2=new MyThread("\n Numero 2", 2);
thread2->SetPriority(WXTHREAD_DEFAULT_PRIORITY);

//se ejecutan los thread creados
thread1->Run();
thread2->Run();

while ( (thread1->Wait()!= 1 and thread1->Wait()!= -1) or (thread2->Wait()!= 1 and thread2->Wait() != -1)){}
return true;
}


Can you help me?[/quote]
Queen

Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor » Tue Jan 17, 2006 6:11 am

Hello.

As thread are based on the same class, why don't you put a static counter of running thread with one or two functions to access it (with mutexes) in order to count how many thread is running and you can wait (while) until all thread have returned.
What is little and green, witch go up and down ??
Yoda playing with the force.

Queen
In need of some credit
In need of some credit
Posts: 8
Joined: Sun Jan 08, 2006 6:17 pm

Post by Queen » Wed Jan 18, 2006 5:07 pm

I'm sorry but I don't undertand you. :(

Why I have to put a static counter?For what? I can't see the concept.

I don't count how many threads is running. I do a loop with a count for print a message only for to know that the programs run well.

Other way I try to do a loop for wait until thread have returned but... It doesn't work.

This is the first time I work with widget and I don't know very well how to use it.

Thanks
Queen

Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor » Wed Jan 18, 2006 5:51 pm

Oh, excuse-me, as I see you write

Code: Select all

thread1->Run();
thread2->Run();
I think you wana use many threads.

What I want to say is you count all thread you run and when it finish, you decrement.
When the count is zero, you can exit the app.

The wait function can not work as you use it because wxThread::wait() (1) wait until the thread stop.

1 : http://www.wxwidgets.org/manuals/2.6.2/ ... threadwait
What is little and green, witch go up and down ??
Yoda playing with the force.

Queen
In need of some credit
In need of some credit
Posts: 8
Joined: Sun Jan 08, 2006 6:17 pm

Post by Queen » Sat Jan 21, 2006 11:39 am

Ok.

Thank you!!
Queen

Locked