|
|
Home » U++ Library support » U++ MT-multithreading and servers » Wait for other Threads
|
|
|
Re: Wait for other Threads [message #46579 is a reply to message #46573] |
Fri, 03 June 2016 08:50 |
|
koldo
Messages: 3356 Registered: August 2008
|
Senior Veteran |
|
|
Hello funky
I am not an expert in multi threading and in my projects I use another focus. However I see two things in your code:
- I would replace while(Thread::GetCount()); with:
while(Thread::GetCount()) {
ProcessEvents();
Sleep(0);
}
I think that with while(Thread::GetCount()); you are locking main thread with an infinite loop, and Guilocks in threads will wait forever.
Calling ProcessEvents(); inside the loop you let the GUI to be refreshed by the threads you have launched.
- intfile
This is a variable handled globally by different threads without any control, so results will be undefined...
A safer way could be to declare intfile as Atomic and increase it with AtomicInc:
Atomic intfile;
...
AtomicInc(intfile);
intfile will behave as an int, but multiple threads will access it safely.
Best regards
Iñaki
[Updated on: Fri, 03 June 2016 08:51] Report message to a moderator
|
|
|
Re: Wait for other Threads [message #46582 is a reply to message #46568] |
Fri, 03 June 2016 09:59 |
mr_ped
Messages: 825 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
I didn't study the original problem, this comment is more a reply to koldo's response.
Operations with int often are naturally atomic even without guards (either completely atomic like ++int, or with possible errors in result value, but undamaged content of int, like int++).
That said, you still can't just increase it in one thread, and display it in second, unless you tell the compiler, that the content can be changed outside of current thread. So you should either use "volatile int" (if you know, what you are doing, and how CPU works), or std::atomic<int> ..
like here is some (a bit too short?) tutorial:
http://baptiste-wicht.com/posts/2012/07/c11-concurrency-tuto rial-part-4-atomic-type.html
|
|
|
Re: Wait for other Threads [message #46587 is a reply to message #46579] |
Sat, 04 June 2016 02:30 |
funky1221
Messages: 3 Registered: May 2016
|
Junior Member |
|
|
I used ProcessEvents() but threads still inside the loop.
just refresh GUI.
If I use PromptOK inside the loop, threads seems to be finished, why ?
But I changed "void testU::Searchfile" to "void Searchfile" and used PostCallback referenced from GuiMT,
it worked.
koldo wrote on Fri, 03 June 2016 08:50Hello funky
I am not an expert in multi threading and in my projects I use another focus. However I see two things in your code:
- I would replace while(Thread::GetCount()); with:
while(Thread::GetCount()) {
ProcessEvents();
Sleep(0);
}
I think that with while(Thread::GetCount()); you are locking main thread with an infinite loop, and Guilocks in threads will wait forever.
Calling ProcessEvents(); inside the loop you let the GUI to be refreshed by the threads you have launched.
- intfile
This is a variable handled globally by different threads without any control, so results will be undefined...
A safer way could be to declare intfile as Atomic and increase it with AtomicInc:
Atomic intfile;
...
AtomicInc(intfile);
intfile will behave as an int, but multiple threads will access it safely.
|
|
|
Goto Forum:
Current Time: Sat Apr 20 05:14:54 CEST 2024
Total time taken to generate the page: 0.02568 seconds
|
|
|