Created
September 24, 2018 11:00
-
-
Save lxfly2000/6f9e779943bcf86ca3ec63d395867fa3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Lua 协程测试 | |
require "alien" | |
alien.kernel32.Sleep:types{ret="void","ulong",abi="stdcall"} | |
function sleep(milliseconds) | |
alien.kernel32.Sleep(milliseconds) | |
end | |
function StartCoSleep() | |
Co_sleep=coroutine.create(function(sleepTimes) | |
print("你想要我睡"..sleepTimes.."次……") | |
for i=1,sleepTimes,1 do | |
print("这是我第"..i.."次睡……") | |
sleep(1000) | |
coroutine.yield(os.date("%X")) | |
end | |
end) | |
end | |
function main() | |
StartCoSleep() | |
for i=1,8,1 do | |
local r,dt=coroutine.resume(Co_sleep,5) | |
if coroutine.status(Co_sleep)=="suspended" then | |
print(r,"你在"..dt.."醒了!") | |
elseif r==true then | |
print(r,"恭喜你已经睡完了!") | |
else | |
print(r,"看样子你已经不想再睡了呢。") | |
end | |
end | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment