Page 1 of 1

wxMessageQueue for Inter-thread communication

Posted: Fri Jun 26, 2009 10:10 am
by Marcus Frenkel
Hi,

I tried the new class wxMessageQueue for inter thread communication that will be available in the next wxWidgets release. Since it is a template it can be used to send arbitrary user data. I tried it with std::string and it works fine, however it doesn't work with std::string array.

I wonder if I do something wrong or it can be used with arrays.

Basically, the message type is std::queue<T>.

I have defined it like this:

Code: Select all

typedef string myStr[4];
wxMessageQueue<myStr[4]> MyQueue;
Then to post message I use:

Code: Select all

string msg[4]={str1,str2,str3,str4};
MyQueue.Post( msg);
On compilation in MS Visual Studio 2008, I don't get any error for the definition, but I get error for using the functions Post and Receive with string array as parameter. The error is below:

Code: Select all

1) : error C2664: 'wxMessageQueue<T>::Post' : cannot convert parameter 1 from 'std::string [4]' to 'const std::basic_string<_Elem,_Traits,_Ax> (&)[4]'
1>        with
1>        [
1>            T=myStr [4]
1>        ]
1>        and
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        Reason: cannot convert from 'std::string [4]' to 'const std::basic_string<_Elem,_Traits,_Ax> [4]'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        There is no context in which this conversion is possible
The class can be seen on this link: http://svn.wxwidgets.org/viewvc/wx/wxWi ... iew=markup

TIA, Marcus

Posted: Fri Jun 26, 2009 12:07 pm
by Frank
you could use std::array or std::vector instead of a naked array.

If you must use a POD-Array, try wxMessageQueue<std::string*> instead.

Posted: Fri Jun 26, 2009 2:20 pm
by Auria
I don't get this part :

Code: Select all

typedef string myStr[4];
wxMessageQueue<myStr[4]> MyQueue;
Unless I'm doing something weird in my head, I understand that this defines mStr to be a string[4]. When you then pass myStr[4] to the template, wouldn't that mean a string[4][4] ? Then when you pass a string[4] it would fail since you declared the template to expect a string[4][4].

Posted: Tue Jun 30, 2009 1:01 am
by Marcus Frenkel
Frank wrote:you could use std::array or std::vector instead of a naked array.

If you must use a POD-Array, try wxMessageQueue<std::string*> instead.
I wanted to go with std::array but it's header is not included in VC++ 2008. I tried to go with simple arrays to avoid the noticeable overhead of using vectors.
Auria wrote: Unless I'm doing something weird in my head, I understand that this defines mStr to be a string[4]. When you then pass myStr[4] to the template, wouldn't that mean a string[4][4] ? Then when you pass a string[4] it would fail since you declared the template to expect a string[4][4].
The way I declared surely doesn't work, but cannot find the right way to do it with naked arrays, it seems there is no way.

Marcus