How can one Tcl thread cause an event in another?

前端 未结 2 1837
故里飘歌
故里飘歌 2021-01-21 10:01

I have a system with multiple I/O interfaces, and I\'m collecting the output of all of them into a common log. Two of the interfaces are through well-behaved channels that are

2条回答
  •  一个人的身影
    2021-01-21 10:54

    I finally grokked what Donal was saying about Thread events. I blame inadequate morning caffeine for not getting it the first time.

    All the prior examples of thread::send I've seen concerned a master sending scripts down to the worker thread. In this case, the worker thread needs to send scripts back up to the master, where the script is the one that would be called on [fileevent readable] if this was a channel.

    Here's my test code for the interaction:

    proc reader {payload} {
        puts $payload
    }
    
    set t1 [thread::create]
    thread::send -async $t1 {
        proc produce {parentid} {
            while 1 {
                after 250   ;# substitutes for
                incr data   ;# the blocking read
                thread::send $parentid "reader $data"
            }
        }
    }
    
    set tid [thread::id]
    thread::send -async $t1 [list produce $tid]
    
    vwait forever
    

    The part I saw but didn't immediately grok was the importance of the master thread having an ID that can be sent to the worker. The key that I'd missed was that the worker can send scripts to the master just as easily as the master can send scripts to the worker; the worker just usually doesn't know the master's Thread ID.

    Once the ID is passed to it, the producer thread can therefore use thread::send to call a proc in the master to handle the data, and it becomes an event in the master thread, just as I'd desired. It's not the way I've worked with threads in the past, but once understood it's powerful.

提交回复
热议问题