Author: Date: Subject:
Jürgen Ladstätter
2008-07-12 19:57:58
synchronizing between threads
hi there,
for my internet application i have a thread that polls a wxSocket and acts
upon the data he gets.
e.g. the socket retrieves a message for the user, my main application opens
a new page (wxNotebook) and displays the message.
everything works fine, except that the application freezes or crashs when
adding a new page. I googl'ed a little bit and found out: wxwidgets is not
threads safe. so now I need a away to tell my main thread to display the
content.
is there a better way then using custom events? can I add custom content to
the event (e.g. the message for the user)? Do you have any example code?
kind regards, juergen
_______________________________________________
wx-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wx-users
Author: Date: Subject:
Hajo Kirchhoff
2008-07-13 09:36:27
Re: synchronizing between threads
Jürgen Ladstätter wrote:
> everything works fine, except that the application freezes or crashs when
> adding a new page. I googl'ed a little bit and found out: wxwidgets is not
> threads safe. so now I need a away to tell my main thread to display the
> content.
to say "wxwidgets is not thread safe" is a bit of an exaggeration.
wxWidgets in general is no more or less thread safe than other libraries.
There is an issue with wxString being not thread safe, which is
noticable when you post a wxCommandEvent from a secondary page. But this
will not apply to you, if you post a different event type.
> is there a better way then using custom events? can I add custom content to
> the event (e.g. the message for the user)? Do you have any example code?
Without any further information from you I can not make any
recommendation. Right now I still believe the error is on your side, not
on the wxWidgets side.
Using custom events is quite okay, as long as you do not post a
wxCommandEvent, which has a wxString member which may be the problem you
are seeing.
Regards
Hajo
_______________________________________________
wx-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wx-users
Author: Date: Subject:
Jürgen Ladstätter
2008-07-13 09:59:42
AW: synchronizing between threads
hello hajo, thanks for your reply.
what information do you need to make a recommendation. I had a look
yesterday on custom events and they look really hard and complicated to
integrate. thought about using a wxCommandEvent - but thanks for your info
that these will fail as well.
The problem is that wxNotebook freezes when adding a page, I dont think
that the error is in my code, because others had the same issue before.
Secondary threads inserting a new page (title is wxString, page is a wxPanel
with sub controls) results into crashes or freezing of the application.
kind regards,
juergen
-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected] Im Auftrag von Hajo Kirchhoff
Gesendet: Sonntag, 13. Juli 2008 09:36
An: [email protected]
Betreff: Re: synchronizing between threads
Jürgen Ladstätter wrote:
> everything works fine, except that the application freezes or crashs when
> adding a new page. I googl'ed a little bit and found out: wxwidgets is not
> threads safe. so now I need a away to tell my main thread to display the
> content.
to say "wxwidgets is not thread safe" is a bit of an exaggeration.
wxWidgets in general is no more or less thread safe than other libraries.
There is an issue with wxString being not thread safe, which is
noticable when you post a wxCommandEvent from a secondary page. But this
will not apply to you, if you post a different event type.
> is there a better way then using custom events? can I add custom content
to
> the event (e.g. the message for the user)? Do you have any example code?
Without any further information from you I can not make any
recommendation. Right now I still believe the error is on your side, not
on the wxWidgets side.
Using custom events is quite okay, as long as you do not post a
wxCommandEvent, which has a wxString member which may be the problem you
are seeing.
Regards
Hajo
_______________________________________________
wx-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wx-users
_______________________________________________
wx-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wx-users
Author: Date: Subject:
Vadim Zeitlin
2008-07-13 17:10:54
Re: AW: synchronizing between threads
On Sun, 13 Jul 2008 09:59:42 +0200 Jürgen Ladstätter <[email protected]> wrote:
JL> what information do you need to make a recommendation. I had a look
JL> yesterday on custom events and they look really hard and complicated to
JL> integrate.
Nevertheless this is the simplest way to implement inter-thread
communications from worker thread to the main one.
JL> The problem is that wxNotebook freezes when adding a page,
You can't perform GUI calls from worker threads. You really do need to
post an event to the main thread asking it to add a new page. Whether you
use a command event or custom event is up to you but command event is less
flexible and doesn't allow you to transport arbitrary data between threads.
Of course, if you just need to add a page maybe you don't need this.
Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
_______________________________________________
wx-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wx-users
Author: Date: Subject:
Hajo Kirchhoff
2008-07-14 12:30:43
Re: AW: synchronizing between threads
Jürgen,
as Vadim said, wxWidgets GUI calls from secondary threads are not
allowed. There are a couple of reasons.
> yesterday on custom events and they look really hard and complicated to
Nah, not at all. Custom events are really very easy to do.
DECLARE_EVENT_TYPE(myEVT_EVENT_CODE, 0)
class wxMyEvent:public wxEvent
{
wxMyEvent():wxEvent(myEVT_EVENT_CODE){}
wxMyEvent* Clone() const { return new wxMyEvent(*this); }
}
DEFINE_EVENT_TYPE(myEVT_EVENT_CODE)
That's about it. Not hard at all.
> Secondary threads inserting a new page (title is wxString, page is a wxPanel
> with sub controls) results into crashes or freezing of the application.
As Vadim said...
But actually there is a general purpose way to make GUI calls from a
secondary thread. You must somehow manage to switch thread contexts.
Basically you send an event to the main thread containing a pointer to a
function you want to execute in the main thread. You then block on a
wxCondition. The main thread processes the event, extracts the pointer
and calls the function. It then posts the result and unblocks the
secondary thread. The secondary thread wakes up, retrieves the result
and continues.
My wxApp derived class has a function:
RunInMainThread(function fnc, int *rc);
My secondary thread wants to make GUI calls. I'll take your example and
add a Notebook page.
// this function works only when called from the main thread
int AddPageToNotebook()
{
notebook->AddPage();
}
Here is the principle. Instead of
void some_function()
{
rc=AddToNotebookPage();
}
you write
void some_function()
{
int rc;
wxGetApp().RunInMainThread(AddPageToNotebook, &rc);
}
This code (RunInMainThread) then 'switches thread contexts' and makes a
call to AddPageToNotebook.
It will work quite well in many cases and is a general purpose
mechanism. The only thing you need to be aware of is that events from
the secondary threads can be posted at any time and so the call to
AddPageToNotebook might happen inbetween some processing in the main
thread where it does not expect this to happen.
Consider the resize event. The notebook is being resized and as a result
a couple of other elements are being sent resize events as well. Usually
no program will add another notebook page during a resize event, unless
you explicitly write a resize handler that adds a notebook page. But if
you have a secondary thread that wants to add a notebook page, it might
happen while resizing.
My general opinion is that if you actually have the need to add a page
from a secondary thread, you should redesign your application.
All my applications (with the exception of the current one) are designed
such that _all_ GUI stuff happens in the main thread. Secondary threads
do not have any user interface at all. What looks like an inconvenience
at first results in better design in the long run IMHO.
Regards
Hajo
_______________________________________________
wx-users mailing list
[email protected]
http://lists.wxwidgets.org/mailman/listinfo/wx-users
|