WxString and Multithread Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
blinde
Earned a small fee
Earned a small fee
Posts: 17
Joined: Wed Oct 07, 2009 9:43 am

WxString and Multithread

Post by blinde »

Hi.

I am using wx 2.8.11 under Linux
I am in a massive multithread soft.

I have some unexplained seg fault (usually related to libc error -> often free unused pointer)

I use some wxString

When Googling, I found some people talking about unsafe functions in multithreading env (especially issues with ref on wxstring).

All my threads are created using pthread lib.
The application is full text.
Wx is just used for wxString or some generic functions.

So the question, in 2.8.11, in my case, can I have some crash due to using some wxString in a multithread env ?


Thanks

Chris
briceandre
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 672
Joined: Tue Aug 31, 2010 6:22 am
Location: Belgium

Post by briceandre »

The answer is yes, you can, but you must take care.

wxStrings use a mechanism of Copy-On-Write, which means that they are not copied when the assignment is performed. So, a code like this is not thread-safe :

Code: Select all

wxString thread_copy;

mutex.Take();
thread_copy = main_str_instance;
mutex.Give();

thread_copy.Strip();
In this kind of code, the real copy will not be performed in the critical section protected with mutex, it will be copied during the Strip operation.

If both threads access the main_str_instance, this can lead to problems...

I am not sure there is a more proper way of avoiding this kind of problem, but you can use code like this :

Code: Select all

wxString thread_copy;

mutex.Take();
thread_copy = wxString::Format("%s", main_str_instance.c_str());
mutex.Give();

thread_copy.Strip();
This will force complete copy of string in critical section and avoid messing-up everything in reference counting mechanisms.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Note that you can simply upgrade to wxWidgets 2.9; under wx 2.9, the ref-counting mechanism was removed, and instead wxString is simply a std::basic_string, which is generally thread-safe
"Keyboard not detected. Press F1 to continue"
-- Windows
briceandre
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 672
Joined: Tue Aug 31, 2010 6:22 am
Location: Belgium

Post by briceandre »

Note that you can simply upgrade to wxWidgets 2.9; under wx 2.9, the ref-counting mechanism was removed, and instead wxString is simply a std::basic_string, which is generally thread-safe
To my best knowledge, the thread-safe status of std::basic_string depends on the implementation, and I am sincerely not sure that the implementation coming with g++ is thread safe ! So, I would use it with great care...
blinde
Earned a small fee
Earned a small fee
Posts: 17
Joined: Wed Oct 07, 2009 9:43 am

Post by blinde »

Hi.

briceandre, thanks for the tip.
I just have to parse my 200000 lines of code and 25 threads to check potential issues !!!

For wx2.9, I had some issues when I tried to migrate...
So up to now, I stay with 2.8.11

Thanks

Chris
bess79bm6
In need of some credit
In need of some credit
Posts: 4
Joined: Tue Jan 11, 2011 9:55 am

Reply

Post by bess79bm6 »

Thank you a lot for this forum, here my little pony I able to get a lot of useful information!
Post Reply