IDL 多线程
通过IDL多线程,进行两个数相加为例
1. 相加函数如下
function add, a, b
return, a+b
end
2. 主线程函数如下,通过新建分线程调用相加函数,如果线程运行结束,则输出结果
pro thread_test
compile_opt idl2
this_pro_path = file_dirname(routine_filepath())
function_position = this_pro_path + '\add.pro'
a = [2,3,4]
b = [20,30,40]
thread_arr = objarr(3)
thread_count = n_elements(thread_arr)
for i = 0, thread_count - 1 do begin
thread = obj_new("IDL_IDLBridge")
thread->execute,".compile '" + function_position + "'"; 编译add.pro代码
thread->setvar,'a', a[i]
thread->setvar,'b', b[i]
thread->execute, "c = add(a, b)", /NOWAIT
print, 'Running... Thread ' + strtrim(i+1, 2)
thread_arr[i] = thread
endfor
stops_arr = intarr(thread_count) ; 存储线程是否运行结束, 1:已经结束, 0:没有结束
while 1 gt 0 do begin
; 判断是否所有线程运行结束
if total(stops_arr) eq thread_count then begin
print, 'Completed!'
break
endif
; 判断并修改线程运行状态,同时输出运行结果
for i = 0, thread_count-1 do begin
thread = thread_arr[i]
is_stops = stops_arr[i]
if is_stops eq 0 and thread->status() eq 0 then begin
stops_arr[i] = 1
print, 'Stops... Thread ' + strtrim(i+1, 2)
c = thread->getvar('c')
print, 'c= ' + strtrim(c, 2)
endif
endfor
endwhile
end
3. IDL运行结果如下
% Compiled module: THREAD_TEST.
Running... Thread 1
Running... Thread 2
Running... Thread 3
Stops... Thread 1
c= 22
Stops... Thread 2
c= 33
Stops... Thread 3
c= 44
Completed!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)