Perl threads 摘要
最近又写了一个多线程的小工具,对一些多线程的使用有了进一步的心得。
Perl 创建线程有两种方式,正常通过threads->create 创建线程,用async 创建一个调用匿名过程的线程,具体参考perldoc threads。线程共享变量需要使用 threads::shared,共享变量只能存储scalar,共享变量的引用,如果存储List Hash的引用需使用shared_clone([@list]) shared_clone({%hash})。线程创建后最好join 或者detach,否则在退出时会有warning。线程的join 方式,使用threads 中提供的函数接口,可以做到及时释放资源,参考下边的例子4 run to the end.1 join? false2 join? false3 join? false3 run to the end.1 join? false2 join? false2 run to the end.1 join? false1 run to the end.