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
WxString and Multithread Topic is solved
-
- Ultimate wxWidgets Guru
- Posts: 672
- Joined: Tue Aug 31, 2010 6:22 am
- Location: Belgium
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 :
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 :
This will force complete copy of string in critical section and avoid messing-up everything in reference counting mechanisms.
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();
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();
-
- Ultimate wxWidgets Guru
- Posts: 672
- Joined: Tue Aug 31, 2010 6:22 am
- Location: Belgium
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...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
Reply
Thank you a lot for this forum, here my little pony I able to get a lot of useful information!