albertodemichelis / squirrel

Official repository for the programming language Squirrel
http://www.squirrel-lang.org
MIT License
895 stars 149 forks source link

Squirrel version of setTimeout,/running code parallel in threads non-blocking #192

Open jsProj opened 4 years ago

jsProj commented 4 years ago

Is there a way we can do this, because i'd like to do something like,

::print(1);
local f1 = (function() {
    for (local i = 0; i < 1000000; i++) {
        ::print(2);
    }
});
local f2 = (function() {
    ::print("Hello world!");
});
local t1 = ::newthread(f1);
local t2 = ::newthread(f2);
t1.call(); // this blocks the rest of the script from running
// the next call should be possible for running parallel and non-blocking, but it doesnt
t2.call(); // executed only after t1 finishes, which takes a few seconds
albertodemichelis commented 4 years ago

A thread will runt until suspend() is called. Squirrel threads are not "OS Threads" are cooperative threads and they must be suspended and resumed explicitly(with suspend and thread.wakeup()).