Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 如何在TCL中正确使用循环启动MultiTheAD?_Multithreading_Tcl - Fatal编程技术网

Multithreading 如何在TCL中正确使用循环启动MultiTheAD?

Multithreading 如何在TCL中正确使用循环启动MultiTheAD?,multithreading,tcl,Multithreading,Tcl,我想使用多线程获得200个文件,因此我修改了一个TCL示例,如下所示。 但结果很奇怪,输出文件的总数是随机的,大约135个。我很困惑线程是如何开始更改变量$thread的值的 package require Thread puts "*** I'm thread [thread::id]" for {set thread 1} {$thread <= 200} {incr thread} { set thread_ida $thread tsv::set app glob

我想使用多线程获得200个文件,因此我修改了一个TCL示例,如下所示。 但结果很奇怪,输出文件的总数是随机的,大约135个。我很困惑线程是如何开始更改变量
$thread
的值的

package require Thread
puts "*** I'm thread [thread::id]"

for {set thread 1} {$thread <= 200} {incr thread} {
    set thread_ida $thread
    tsv::set app global_thread_num $thread_ida

    set id [thread::create -joinable {

        puts [ tsv::get app global_thread_num ]
        set thread_id [ tsv::get app global_thread_num ]
        puts "${thread_id}thread_id"
        set outFile "./test/${thread_id}"
        append outFile ".tmd"
        puts $outFile
        set FileOut [open $outFile w+]
        puts $FileOut "${thread_id}thread_id"
    }] ;# thread::create
    puts "*** Started thread $id"
    lappend threadIds $id
} ;# for
puts "*** Existing threads: [thread::names]"
# Wait until all other threads are finished
foreach id $threadIds {
    thread::join $id
}
puts "*** That's all, folks!"
包需要线程
放置“***我是线程[thread::id]”

对于{set thread 1}{$thread来说,您遇到的问题是以下两行:

puts [ tsv::get app global_thread_num ]
set thread_id [ tsv::get app global_thread_num ]
不保证获得相同的值,也不可能与外部循环中共享变量的设置同步。Tcl中的线程在启动期间具有合理的开销

相反,您应该做的是在一个过程中创建带有工作描述的线程,然后向它们发送一条带有ID的简单消息来启动真正的处理;这样做更容易工作

package require Thread
puts "*** I'm thread [thread::id]"

for {set thread 1} {$thread <= 200} {incr thread} {
    set id [thread::create -joinable {
        proc DoWork {thread_id} {
            # Only one puts here
            puts "${thread_id}thread_id"
            set outFile "./test/${thread_id}"
            append outFile ".tmd"
            puts $outFile
            set FileOut [open $outFile w+]
            puts $FileOut "${thread_id}thread_id"
            # Close the channel, please...
            close $FileOut
            # Thread done, and since we're using joinable threads it should die now
            thread::release
        }
        thread::wait
    }] ;# thread::create
    puts "*** Started thread $id"
    lappend threadIds $id
    # Start the work going, passing over the numeric ID in the "message"
    thread::send -async $id [list DoWork $thread]
} ;# for
puts "*** Existing threads: [thread::names]"
# Wait until all other threads are finished
foreach id $threadIds {
    thread::join $id
}
puts "*** That's all, folks!"

您遇到的问题是这两条线:

puts [ tsv::get app global_thread_num ]
set thread_id [ tsv::get app global_thread_num ]
不保证获得相同的值,也不可能与外部循环中共享变量的设置同步。Tcl中的线程在启动期间具有合理的开销

相反,您应该做的是在一个过程中创建带有工作描述的线程,然后向它们发送一条带有ID的简单消息来启动真正的处理;这样做更容易工作

package require Thread
puts "*** I'm thread [thread::id]"

for {set thread 1} {$thread <= 200} {incr thread} {
    set id [thread::create -joinable {
        proc DoWork {thread_id} {
            # Only one puts here
            puts "${thread_id}thread_id"
            set outFile "./test/${thread_id}"
            append outFile ".tmd"
            puts $outFile
            set FileOut [open $outFile w+]
            puts $FileOut "${thread_id}thread_id"
            # Close the channel, please...
            close $FileOut
            # Thread done, and since we're using joinable threads it should die now
            thread::release
        }
        thread::wait
    }] ;# thread::create
    puts "*** Started thread $id"
    lappend threadIds $id
    # Start the work going, passing over the numeric ID in the "message"
    thread::send -async $id [list DoWork $thread]
} ;# for
puts "*** Existing threads: [thread::names]"
# Wait until all other threads are finished
foreach id $threadIds {
    thread::join $id
}
puts "*** That's all, folks!"

我假设您实际使用的工作负载更复杂;只需在一个线程中为200个文件中的每一个文件编写一行代码就更容易了。我将我的tcl文件修改为您的,它可以工作。非常感谢~我终于发现了我的错误~哈哈。我使用线程来做一些繁重的工作,而不仅仅是为200个文件中的每一个文件编写一行代码。我“我假设您实际使用的工作负载更复杂;仅在一个线程中为200个文件中的每一个文件编写一行代码更容易完成。我将我的tcl文件修改为您的,它可以工作。非常感谢~我终于发现了我的错误~哈哈。我使用线程来完成一些繁重的工作,而不仅仅是为200个文件中的每一个文件编写一行代码。