|
Prev: License manager
Next: How do you use __WXFUNCTION__?
From: mailinglists on 19 Apr 2008 03:20 Hi all, I've found an old discussion from 2002 on wx-dev essentially saying that wxString is not thread safe, i.e. I cannot share a wxString between threads. Is this still true? I am using AddPendingEvent to post a wxString object from one thread to another and was under the impression that this is a safe thing to do. But I do get random crashes and the wxStringData reference counting mechanism does not contain a thread synchronisation mechanism. I am using wxWidgets 2.8.7 with default setup.h If wxString is still not thread safe then a note at both wxString and AddPendingEvent mentioning this would certainly be appreciated, or have I missed this information? This is especially true with AddPendingEvent, since AddPendingEvent is the 'official' method of posting events from one thread to another. I am using a wxCommandEvent and call wxCommandEvent::SetString to post the string. Looked perfectly good to me. Best regards Hajo
From: Eran Ifrah on 19 Apr 2008 03:54 When calling AddPendingEvent, wx clones the original event (creates a new instance of it), so if u are using SetString(), it will work perfectly, since AddPendingEvent passes a different instance of the event (thus, different instance of the original string...). So I guess your crash comes from somewhere else in the code Eran On Sat, Apr 19, 2008 at 10:20 AM, mailinglists < mailinglists(a)hajo-kirchhoff.de> wrote: > Hi all, > I've found an old discussion from 2002 on wx-dev essentially saying that > wxString is not thread safe, i.e. I cannot share a wxString between threads. > > Is this still true? > > I am using AddPendingEvent to post a wxString object from one thread to > another and was under the impression that this is a safe thing to do. > > But I do get random crashes and the wxStringData reference counting > mechanism does not contain a thread synchronisation mechanism. > > I am using wxWidgets 2.8.7 with default setup.h > > If wxString is still not thread safe then a note at both wxString and > AddPendingEvent mentioning this would certainly be appreciated, or have I > missed this information? > > This is especially true with AddPendingEvent, since AddPendingEvent is the > 'official' method of posting events from one thread to another. I am using a > wxCommandEvent and call wxCommandEvent::SetString to post the string. Looked > perfectly good to me. > > Best regards > > Hajo > _______________________________________________ > wx-users mailing list > wx-users(a)lists.wxwidgets.org > http://lists.wxwidgets.org/mailman/listinfo/wx-users > -- Eran Ifrah eran.ifrah(a)gmail.com
From: Vadim Zeitlin on 19 Apr 2008 08:17 On Sat, 19 Apr 2008 09:20:02 +0200 mailinglists <mailinglists(a)hajo-kirchhoff.de> wrote: m> I've found an old discussion from 2002 on wx-dev essentially saying that m> wxString is not thread safe, i.e. I cannot share a wxString between m> threads. m> m> Is this still true? Yes. m> I am using AddPendingEvent to post a wxString object from one thread to m> another and was under the impression that this is a safe thing to do. No, it isn't. You need to use s.c_str() or something like this to ensure that the string is really copied when you use it with AddPendingEvent() and not just shared. m> If wxString is still not thread safe then a note at both wxString and m> AddPendingEvent mentioning this would certainly be appreciated We probably should mention this more prominently somewhere but it's not clear where to put it to make sure it's easy to notice... Regards, VZ -- TT-Solutions: wxWidgets consultancy and technical support http://www.tt-solutions.com/
From: mailinglists on 20 Apr 2008 10:46 Thanks for the responses. > cannot share a wxString between > m> threads. > m> > m> Is this still true? > > Yes. > > m> I am using AddPendingEvent to post a wxString object from one thread to > m> another and was under the impression that this is a safe thing to do. > > No, it isn't. You need to use s.c_str() or something like this to ensure > that the string is really copied when you use it with AddPendingEvent() and > not just shared. > That will not work either. If I use s.c_str() and pass it to AddPendingEvent, AddPendingEvent will still create a wxString from the c_str() and then _that_ string is shared between threads. The crash will still occur, although more rarely (I've tried it). If wxString is not threadsafe, one cannot use AddPendingEvent at all to pass a wxString. The only safe way I see is to create a wxString (or C-String) on the heap, cast the pointer to an integer, pass that integer to AddPendingEvent. Or create my own event containing a std::string or char*. > m> If wxString is still not thread safe then a note at both wxString and > m> AddPendingEvent mentioning this would certainly be appreciated > > We probably should mention this more prominently somewhere but it's not > clear where to put it to make sure it's easy to notice... > How about both in the wxString and the AddPendingEvent documentation. Best regards Hajo > Regards, > VZ > > -- TT-Solutions: wxWidgets consultancy and technical support > http://www.tt-solutions.com/ > _______________________________________________ wx-users mailing list > wx-users(a)lists.wxwidgets.org > http://lists.wxwidgets.org/mailman/listinfo/wx-users
From: mailinglists on 21 Apr 2008 02:58
Hi Vadim, > Sorry, I don't understand why does it occur, maybe it's a different crash? > To be precise, I don't see where is the string created inside > AddPendingEvent() shared between threads? > the wxCommandEvent wxString object is shared between threads. Here is the code: wxCommandEvent evt; evt.SetString(myString.c_str()); AddPendingEvent(evt); While my original string 'myString' will not be shared, the wxString object inside wxCommandEvent will be. AddPendingEvent calls evt.Clone() which creates a copy of the wxCommandEvent::wxString object. This copy is accessed in the primary thread while the original wxString remains in the secondary thread. Both string objects share the same wxStringData. If the primary thread runs before the destructors in the secondary thread destroy my wxCommandEvent object, the string will be shared between the threads. AddPendingEvent protects the event queue with mutex/critical secions, but immediately after the event has been inserted into the queue and the locks have been released, the primary thread is allowed to run and on modern dual core systems it will run almost immediately and concurrently. At this time the wxCommandEvent::wxString object still exists. I am almost 100% certain that this is what happens. Best regards Hajo > Regards, > VZ > > |