in reply to Re^5: Annoying threads share problem!
in thread Annoying threads share problem!

It's possible to do, but I'm not entirely sure what kind of code you are after? From your description of the problem, I'm guessing you want the @choices available at the listbox to change in main, when the thread adds another element to @choices?

If so, make @choices shared, and run a timer in main, which saves the old @choices and compares it to the current @choices, and updates the listbox when it detects a difference.

Alternatively, you could setup a few extra shared variables, one a flag, and one the new entry. When the thread wants to add a new entry to @choices, it sets the flag, and the new data to add.

Pretty much, either way, you are going to need a timer running in the main Tk thread to periodically check for changes. Tk will not watch across thread boundaries for variable changes, you have to actively read them.

Another idea, is setup some sort of binding on the listbox, that updates it's selections from @choices, everytime it comes into focus, or when the drop-down-selection list is triggered.


I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^7: Annoying threads share problem!
by Ace128 (Hermit) on Oct 10, 2005 at 12:25 UTC
    Yea, this is the current solution. I have some flag that the main (tk) thread checks every 20 ms or so... updates @choices when flag set.. Although I would prefer that the searchUpdate thread notified the main thread somehow, this works just fine. But seems like such a CPU waste (although its not many instructions... some jump and a check and jump back).
      I havn't messed with it lately, and didn't experiment across threads with it, but you might be looking for using shared memory. See "perldoc perlfunc /shmget" and "perldoc perlipc /SysV" . What it does is setup memory segments which can be directly read by separate processes. It requires handling alot of details, but it is supposedly the fastest IPC. Check out Tk-shared-mem-ipc for how to use it it with Tk.

      I'm not really a human, but I play one on earth. flash japh
        Thanks! Too bad this only for Linux!