in reply to Re^2: Perl/Tk Multithreading
in thread Perl/Tk Multithreading
I tried to follow your description of the problem, but it still isn't clear that you are confining all of your network code to the worker thread as you seem to be creating the Device::Modem::GSM object in your keypress subroutine, which I assume is in your main thread.
The problem does seem familiar to me though, as I had to work thru similar issues with various threaded programs. The general safe rule is only communicate with the thread thru shared variables, or options passed to the thread during thread->create. That means if you receive a keypress in your main thread, transfer it to a shared variable designed to hold it, then somehow get the thread to read the information from the shared variable, and do what needs to be done.
So your use Device::Modem::GSM; statement, as well as my $mod= new Device::Modem::GSM() statement, should be confined to the thread, and your keypresses should be passed in thru a shared variable.
If you havn't figured it out yet, by seeing it mentioned in numerous posts in the archives, but Perl threads are created by a copy-on-create method, which makes an exact copy of the parent thread at the time of creation. So, when you create a Device::Modem::GSM object in the main thread, after the worker thread gets created, the worker thread has to use try and access the object in the main thread, crossing a safety barrier, and it is the source of all sorts of errors like you are getting. Or, if you create the Device::Modem::GSM() object before the thread is created, there are essentially 2 objects, 1 in main, and a copy in the thread... that causes boundary errors too.
I know you probably have invested quite a bit of time into your Tk frontend to this, but you may want to switch to Gtk2, because it has improved thread safety features, and can use subs which are common amoung threads, thru it's Glib::Idle->add method.
But if you stick with Tk, make sure ALL network code is confined to the worker thread, and only communicate with the thread thru pre-defined shared variables. That means in your worker thread, you will need a loop of some sort to pick up the keypresses which your main thread has put in a shared variable. Don't try to pass the sub created in main, with options to the thread.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Perl/Tk Multithreading
by mahis_431 (Novice) on Jul 28, 2011 at 13:37 UTC |