wxThread and wxString::FromUTF8

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

wxThread and wxString::FromUTF8

Post by Marcus Frenkel »

Hi,

In my secondary thread I have a function like this:

Code: Select all

void Function(std::string & result ) {
  wxString Result = wxString::FromUTF8( result.c_str() );
  wxCommandEvent *event = new wxCommandEvent(wxEVT_COMMAND_MENU_SELECTED,ID_function1);
  event->SetString(Result);
  wxQueueEvent(frame, event);

}
Is it safe to use the wxString::FromUTF8 function in multiple threads at the same time?

Or it's safer to use wxString Result(result.c_str(), wxConvUTF8);?

Thx, Marcus
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

wxString is not thread safe.
I suggest you to send a wxChar* buffer, and delete it in the handler :

Code: Select all

wxChar* buffer = new wxChar[Result.Length() + 1];
wxStrcpy(buffer, Result.c_str());
buffer[Result.Length()] = 0;
event.SetClientData(buffer);
...


// In the handler
wxChar* buffer = (wxChar*) event.GetClientData();
wxString Result(buffer);
delete[] buffer;
Jérémie
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

Please read:
http://docs.wxwidgets.org/trunk/classwx ... ef0318b59a

While wxString is not thread-safe, the way you're using wxString::FromUTF8 is ok.

However, if you use wxCommandEvent, you should use

Code: Select all

event->SetString( Result.c_str() );
Alternatively, use wxThreadEvent instead (like shown in above link).
Use the source, Luke!
Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Post by Marcus Frenkel »

doublemax wrote:While wxString is not thread-safe, the way you're using wxString::FromUTF8 is ok.
I thought that multiple threads cannot call the same global function. Since wxString::FromUTF8 is a static function it actually acts as a global function, am I missing something?
doublemax wrote:However, if you use wxCommandEvent, you should use

Code: Select all

event->SetString( Result.c_str() );
Is it neceserry to make deep copy twice? Since it is already done once in the conversion from string to wxString as shown below:

Code: Select all

wxString Result = wxString::FromUTF8( result.c_str() );
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

I thought that multiple threads cannot call the same global function.
That depends on whether the called function is thread safe or not.
Since wxString::FromUTF8 is a static function it actually acts as a global function, am I missing something?
Now that you mention it, i may have read somewhere that the CSConv classes are not thread safe, i don't know if this is (still) true. So, if you want to be safe, you will have to implement your all small helper class with a mutex to ensure thread safety.
Is it neceserry to make deep copy twice? Since it is already done once in the conversion from string to wxString as shown below:
In this particular case you're right, because the string object goes out of scope immediately. But i'd always enforce deep copy at the latest possible stage and not rely on the fact that it's ensured somewhere earlier in the code.
Use the source, Luke!
Post Reply