(不知何故我错过了这个线程)
首先,sleep/1
不要time_out/3
蜂拥而至。 time_out/3
等待一定数量的 CPU 时间或线程时间毫秒,而sleep/1
实时睡眠秒。与SICStus中的原始版本相比,YAP 存在一定的限制,请参阅手册了解更多信息。
在来自 git 的当前开发版本中(最后一次提交 d5ce9a137668fe1ae34e2d47c91fc2725ae04a5f,日期:Wed Apr 22 14:21:20 2015 -0600),该机制似乎被破坏了。
在 6.3.4 的旧版本中,我得到:
YAP 6.3.4 (x86_64-linux): Tue Jan 29 12:39:29 CET 2013
MYDDAS version MYDDAS-0.9.1
?- use_module(library(timeout)).
% reconsulting /opt/gupu/share/Yap/timeout.yap...
% reconsulting /opt/gupu/share/Yap/hacks.yap...
% reconsulted /opt/gupu/share/Yap/hacks.yap in module yap_hacks, 0 msec 15472 bytes
% reconsulted /opt/gupu/share/Yap/timeout.yap in module timeout, 0 msec 48240 bytes
true.
?- 100000000=J,time(time_out((between(1,J,N),N=J),1,R)).
% 0.008 CPU in 0.007 seconds (114% CPU)
J = 100000000,
R = time_out.
?- 100000000=J,time(time_out((between(1,J,N),N=J),10000,R)).
% 9.985 CPU in 10.003 seconds ( 99% CPU)
J = 100000000,
R = time_out.
?- 100000000=J,time(time_out((between(1,J,N),N=J),100000,R)).
% 23.477 CPU in 23.522 seconds ( 99% CPU)
J = N = 100000000,
R = success.
?- 100000000=J,time(time_out(sleep(10),1,R)).
% 0.000 CPU in 10.000 seconds (Inf% CPU)
J = 100000000,
R = success.