为什么我的tcl线程在:: thread :: wait存在时退出?

时间:2016-09-12 14:20:54

标签: multithreading tcl

我试图理解线程的工作:从下面的代码中等待

set logger [thread::create {
   proc OpenLog {file} {
   global fid
   set fid [open $file a]
} proc CloseLog {} {
   global fid
   close $fid
} proc AddLog {
   msg} {
   global fid
   puts $fid $msg
} thread::wait
}]

% ::thread::exists $logger
0

为什么上面的代码不等待均匀并立即退出?

1 个答案:

答案 0 :(得分:2)

问题是你的线程创建脚本中有一些语法错误,导致它无法正常启动;它以异步方式死亡并输出错误消息。你的情况似乎缺少这个错误;不明白为什么,但它应该读取类似的东西:

Error from thread tid0x100481000
wrong # args: should be "proc name args body"
    while executing
"proc OpenLog {file} {
   global fid
   set fid [open $file a]
} proc CloseLog {} {
   global fid
   close $fid
} proc AddLog {
   msg} {
   global fid..."

如果我们纠正明显的语法问题,将空格转换为重要的新行,那么我们可以得到这个似乎对我有用的内容:

//This class implements Runnable and is used as thread.
//making it so every function in the class can read and edit this var.
private DataRegister dr;

//this runs when the user decides to make a new Database
//the user can max. have 1 DB
//only fires if dr == null
public void makeDB(){
    dr = new DataRegister();
    dr.clearRegister();
}

//this runs whenever the user decides to remove the database.
//the user is unable to user this function atm.
//only fires if dr != null
public void removeDB(){
    dr = null;
}

//fires whenever the user wants to create a new object of type Data
public void createData(){
    ...code code code and more code
    //user creates the object by entering info in a GUI and when the user
    //presses "confirm" the code will read the info and make a new Object of 
    //type Data in a var _tempData.
    dr.addData(_tempData); 
}

public void drawContent(){
    ... does a lot of things with the GUI
    ArrayList<Data> _data = dr.getData();
    System.out.println(_data.size());
    //this outputs 0 2 times because the getData() also prints it in console
}

唯一的区别是空白。 Tcl关心空白。做对了。