Discussion:
wxThreadHelper: Min Sleep(milliseconds) in the while (1) of the thread Entry
Mahmoud Eltahawy
2014-04-28 13:19:47 UTC
Permalink
Hi All,
I have awhile(1) loop in the entry of wxThreadHelper which continuously
reads data from Socket then send an event to Main Gui Thread to update the
GUI,
To increase the performance as possible, I am asking about the min time to
sleep (wxThread::sleep(x)) inside the Entry loop

wxThread::ExitCode AppFrame::Entry()
{
wxThreadEvent evt( wxEVT_THREAD, PROTOCOL_BG_THREAD );
while(1) {
if(!GetThread()->TestDestroy())
break;
/* Read from socket */
wxQueueEvent(this,evt.Clone() );
wxThread::Sleep(...);
}
return (wxThread::ExitCode)0;

}

Thanks
Mahmoud
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Vadim Zeitlin
2014-04-28 14:48:13 UTC
Permalink
On Mon, 28 Apr 2014 06:19:47 -0700 (PDT) Mahmoud Eltahawy wrote:

ME> I have awhile(1) loop in the entry of wxThreadHelper which continuously
ME> reads data from Socket then send an event to Main Gui Thread to update the
ME> GUI,
ME> To increase the performance as possible, I am asking about the min time to
ME> sleep (wxThread::sleep(x)) inside the Entry loop
ME>
ME> wxThread::ExitCode AppFrame::Entry()
ME> {
ME> wxThreadEvent evt( wxEVT_THREAD, PROTOCOL_BG_THREAD );
ME> while(1) {
ME> if(!GetThread()->TestDestroy())
ME> break;
ME> /* Read from socket */
ME> wxQueueEvent(this,evt.Clone() );
ME> wxThread::Sleep(...);
ME> }
ME> return (wxThread::ExitCode)0;
ME>
ME> }

There is no fixed value which would work in all cases. Instead you should
adapt to the speed at which the events are handled in the main thread, e.g.
keep a counter of the pending events and not generate any new ones until it
decreases below some threshold (possibly to 0).

It would be nice to have some high level mechanism for it, e.g. some
wxRateLimitedEventSender, as this is a common problem and the solution is
not that simple. But it's difficult to create such helper because it would
need to be very flexible, e.g. it should allow you to configure what
happens when there are too many, what exactly constitutes too many and so
on, so it's rather complex. If anybody has any good ideas about this, it'd
be interesting to discuss them.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Mahmoud Eltahawy
2014-04-28 16:09:35 UTC
Permalink
Hi Vadim,
Thanks for your help, I tried to do a blocking socket read inside the
wxThreadHelper but after sometimes of reading data, the gui crushes, here
is the gdb output
Is there any issue for read from socket(blocking) in helper thread? Should
I use the basic secondary thread instead?

gdb:

(App_tracer:29513): GLib-CRITICAL **: g_source_attach: assertion
`!SOURCE_DESTROYED (source)' failed

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x40a00940 (LWP 29679)]
0x0000003e7ec08d10 in pthread_mutex_lock () from /lib64/libpthread.so.0
(gdb) up
#1 0x0000003e8282c6f1 in ?? () from /lib64/libglib-2.0.so.0
(gdb)
#2 0x0000003e82825260 in g_io_add_watch_full () from
/lib64/libglib-2.0.so.0
(gdb)
#3 0x0000000000789fdf in GTKFDIOManager::AddInput (this=0xca18e8,
handler=0x1096998, fd=11, d=INPUT) at ./src/gtk/sockgtk.cpp:54
54 handler);
(gdb)
#4 0x00000000005535c4 in wxSocketFDBasedManager::Install_Callback
(this=0xc977b0, socket_=0x1096920, event=wxSOCKET_INPUT) at
./src/common/socketiohandler.cpp:62
62 fd = m_fdioManager->AddInput(socket, socket->m_fd, d);
(gdb)
#5 0x0000000000553a87 in wxSocketImplUnix::DoEnableEvents (this=0x1096920,
flags=1, enable=true) at ./src/unix/sockunix.cpp:108
108 manager->Install_Callback(this, wxSOCKET_INPUT);
(gdb)
#6 0x0000000000553e94 in wxSocketImplUnix::EnableEvents (this=0x1096920,
flags=1) at ./include/wx/unix/private/sockunix.h:80
80 { DoEnableEvents(flags, true); }
(gdb)
#7 0x0000000000553eef in wxSocketImplUnix::ReenableEvents (this=0x1096920,
flags=1) at ./include/wx/unix/private/sockunix.h:53
53 EnableEvents(flags);
(gdb)
#8 0x000000000055281a in wxSocketReadGuard::~wxSocketReadGuard
(this=0x409fea80, __in_chrg=<value optimized out>) at
./src/common/socket.cpp:221
221 impl->ReenableEvents(wxSOCKET_INPUT_FLAG);
(gdb)
#9 0x000000000054fc59 in wxSocketBase::Read (this=0x109fe90,
buffer=0x409feb29, nbytes=83) at ./src/common/socket.cpp:956
956 return *this;
(gdb)
#10 0x000000000044a932 in AppReadPacketFromSocket(wxSocketBase *,
<anonymous struct> *) (pSock=0x109fe90, pPkt=0x409feb10) at
AppControls.cxx:542
542 pSock->Read(pPkt->aPayload,pPkt->Header.wLength);
(gdb)

Can you please help?

Thanks
Mahmoud
Post by Mahmoud Eltahawy
Hi All,
I have awhile(1) loop in the entry of wxThreadHelper which continuously
reads data from Socket then send an event to Main Gui Thread to update the
GUI,
To increase the performance as possible, I am asking about the min time to
sleep (wxThread::sleep(x)) inside the Entry loop
wxThread::ExitCode AppFrame::Entry()
{
wxThreadEvent evt( wxEVT_THREAD, PROTOCOL_BG_THREAD );
while(1) {
if(!GetThread()->TestDestroy())
break;
/* Read from socket */
wxQueueEvent(this,evt.Clone() );
wxThread::Sleep(...);
}
return (wxThread::ExitCode)0;
}
Thanks
Mahmoud
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Vadim Zeitlin
2014-04-28 16:13:12 UTC
Permalink
On Mon, 28 Apr 2014 09:09:35 -0700 (PDT) Mahmoud Eltahawy wrote:

ME> Thanks for your help, I tried to do a blocking socket read inside the
ME> wxThreadHelper but after sometimes of reading data, the gui crushes, here
ME> is the gdb output

This doesn't look like blocking read, events/callbacks shouldn't be used
when the socket is in blocking mode... But then there have been several
reports about bugs in wxSocket when using it from other threads and I never
had time to fix them properly. Try checking them in wxTrac, some of them
had candidate patches or suggestions for workaround in them.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Loading...