防止tcl线程被主事件循环阻塞 [英] Prevent tcl thread from being blocked by main event loop

查看:130
本文介绍了防止tcl线程被主事件循环阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连续运行一个线程,并且不希望它被tcl主事件循环阻塞. 这是我要执行的操作的一个简单示例:

I am trying to run a thread continuously and not have it become blocked by the tcl main event loop. Here is a simple example of what I'm trying to do:

#!/bin/sh
#\
exec tclsh "$0" "$@"

package require Thread

set ::a_thread [thread::create {thread::wait}]

proc start_a {} {
  thread::send $::a_thread {
    puts "Running a thread"
  }
  after 1000 a_start
}

proc infinite_loop {} {
  while {1} {
    puts "Loop"
    after 500
  }
}

start_a
infinite_loop

vwait forever

在此代码中,调用infinite_loop proc,并且主事件循环无限运行.我想如果a_thread仍然可以在后台运行.我该如何实现?

In this code, the infinite_loop proc is called and the main event loop runs infinitely. I would like it if the a_thread could still run in the background though. How can I achieve this?

推荐答案

主事件循环未阻止您的线程.相反,您使用主事件循环来处理将在线程中执行的脚本.而是在线程本身中运行调度程序:

The main event loop is not blocking your thread. Instead you are using the main event loop to shedule scripts to be executed in the thread. Instead, run the scheduler in the thread itself:

代码已测试并且可以按预期工作:

thread::send $::a_thread {
    proc loop {} {
        puts "running a thread"
        after 1000 loop
    }
    loop
}

while 1 {
    puts "loop"
    after 500
}

这篇关于防止tcl线程被主事件循环阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆