[wxWidgets]_[初级]_[发送异步事件的注意项之字符串深浅复制]
2013-04-28 01:02
239 查看
1.在工作线程和主线程进行通讯时,一般是通过发送事件来同步数据,而工作线程只能发送异步事件,基本不能发送界面绘制相关的同步事件。通讯数据时大多情况下需要传递字符串数值,这时候就需要先使用wxEvent的SetString方法,再使用QueueEvent.这里注意了,异步事件是对wxString有所有权的.
以下摘录wx.chm文档里的api说明.
QueueEvent() can be used for inter-thread communication from the worker threads to the main thread, it is safe in the sense that it uses locking internally and avoids the problem mentioned in AddPendingEvent() documentation by ensuring that the event object
is not used by the calling thread any more. Care should still be taken to avoid that some fields of this object are used by it, notably any wxString members of the event object must not be shallow copies of another wxString object as this would result in them
still using the same string buffer behind the scenes. For example:
wxCommandEvent 普通事件:
wxThreadEvent 线程事件:
2.wxString里面其实就是std::string,用std::string时也要注意点。
以下摘录wx.chm文档里的api说明.
QueueEvent() can be used for inter-thread communication from the worker threads to the main thread, it is safe in the sense that it uses locking internally and avoids the problem mentioned in AddPendingEvent() documentation by ensuring that the event object
is not used by the calling thread any more. Care should still be taken to avoid that some fields of this object are used by it, notably any wxString members of the event object must not be shallow copies of another wxString object as this would result in them
still using the same string buffer behind the scenes. For example:
wxCommandEvent 普通事件:
void FunctionInAWorkerThread(const wxString& str) { wxCommandEvent* evt = new wxCommandEvent; // NOT evt->SetString(str) as this would be a shallow copy 浅复制,就是复制了计数器 evt->SetString(str.c_str()); // make a deep copy 深复制,连数据一起创建了一个copy. wxTheApp->QueueEvent( evt ); }
wxThreadEvent 线程事件:
void FunctionInAWorkerThread(const wxString& str) { wxThreadEvent evt; evt->SetString(str); // wxThreadEvent::Clone() makes sure that the internal wxString // member is not shared by other wxString instances: wxTheApp->QueueEvent( evt.Clone() ); }
2.wxString里面其实就是std::string,用std::string时也要注意点。
string( const string& s ); // a copy of the given string s, 浅复制 string( const char* str ); // a duplicate of str (optionally up to length characters long), 深复制
相关文章推荐
- [wxWidgets]_[初级]_[发送异步事件的注意项之字符串深浅复制]
- 事件复制[wxWidgets编程系列2]_[发送异步事件的注意项之字符串深浅复制]
- 【Python初级】由判定回文数想到的,关于深浅复制,以及字符串反转的问题
- [wxWidgets]_[初级]_[监听wxListCtrl和wxGrid的滚动条事件]
- [wxWidgets]_[初级]_[监听wxListCtrl和wxGrid的滚动条事件]
- opencl:异步复制函数的注意事项(async_work_group_copy/async_work_group_strided_copy)
- HTML 5 服务器发送事件注意事项
- 字符串复制注意问题
- 03字符串拼接、格式化输出、深浅复制
- 关于winsock中网络编程事件模型和窗口消息模型中FD_WRITE的理解与数据的发送需要注意的关键点
- [*注意指向常量的字符指针变量*]用指向常量的字符指针变量和字符数组方法把字符串a复制到字符串b中
- MySQL 5.5 主从复制异步、半同步以及注意事项详解
- 字符串拼接、格式化输出、深浅复制
- 异步SOCKET编程-发送和接收数据异步事件触发
- [wxWidgets]_[初级]_[反转wxString字符串]
- ajax发送异步请求时的注意点
- [wxWidgets]_[初级]_[反转wxString字符串]
- MySQL 5.5 主从复制异步、半同步以及注意事项详解
- python基本数据类型(三)-字符串拼接-格式化输出-深浅复制-python3笔记
- 关于winsock中网络编程事件模型和窗口消息模型中FD_WRITE的理解与数据的发送需要注意的关键点