Jump to content
Sign in to follow this  
tortuosit

spawn or execvm?

Recommended Posts

Hi guys,

in my weather mod I use independent rain/wind threads. I call it thread, what I mean is an independently running script, usually running in an endless loop. From the main script I simply called them via "rainhandle=execvm 'scriptwithwhileloop'". I don't know of what data type rainhandle is, but least I can say I was able to kill the threads via this handle, I also need to reliably kill threads. This is usually done via "terminate" command or setting a binary global variable (inside the script there is usually "while (a_globalvariable) do {"

BTW, How can I determine if a thread is running? I hoped there would be some kind of taskmanager. Well I only could see it with producing some output from these threads. No output=thread dead...

OK, now I often see people using "[] spawn {usually_a_loop}". Should I go this way?

Important point is performance. What's better? As less loops as possible? sleep() as long as possible?

Thanks for your input :)

Share this post


Link to post
Share on other sites

From the order I identified questions from:

1. You can always stop a script from running using terminate, provided you know the script handle.

2. scriptDone and you can also use scriptNull to determine whether a script has ever run or not

3. It depends on what you want to do, if you need a constant update, use a loop with no sleep. If the results don't matter as much, you can use a loop with sleep.

Share this post


Link to post
Share on other sites

These are the same thing

_handle = [] execVM "script1.sqf":
_handle = [] spawn {_this call compile preProcessFileLineNumbers "script1.sqf"}

To see if a thread is still running you can use scriptDone

As to your last question: it depends entirely on what your loops are doing, and what for.

Share this post


Link to post
Share on other sites

It's also important to note that if you run a script or spawn a thread, the code will run until the end then terminate itself. That's the reason most people, myself included, will use an infinite loop. It's also usually for testing/debug purposes

Share this post


Link to post
Share on other sites

Small question: How many spawn-threads can be opened in Arma 3? Is there any number or any maximum? I guess its depending on servers hardware is it?

Share this post


Link to post
Share on other sites

Thanks for the hint about scriptDone and scriptNull.

Just for the theory. What if I do:

_handle = [args] execVM "script1.sqf";

and later again

_handle = [args] execVM "script1.sqf";

without having terminated the first one? I'd have no chance to stop the first incarnation of script1.sqf manually. Is that correct? I haven't got the handle of the first one and therefore there is no way to stop it. Thats also a reason why I use "while {youareallowedtorun} do {" - so I can set youareallowedtorun=false from outside and the loop would stop after all sleeping has ended.

_handle = [] spawn {_this call compile preProcessFileLineNumbers "script1.sqf"}

Ah I see, with spawn I could open a new thread and keep the code inline... may be useful thx.

Edited by tortuosit

Share this post


Link to post
Share on other sites
_handle = [args] execVM "script1.sqf";

and later again

_handle = [args] execVM "script1.sqf";

The second execVM would overwrite the first handle (if that script is still running).

If you want to avoid that you could also save your script-handles in an array.:

_handles = [];

_handles pushback ( [args] execVM "script1.sqf" );

and later again

_handles pushback ( [args] execVM "script1.sqf" );

_handle = [] spawn {_this call compile preProcessFileLineNumbers "script1.sqf"}

Keep in mind that this is only an example, so don't use that construct!

execVM does exactly the same and spawn only makes sense when you have inline code.

Share this post


Link to post
Share on other sites
Small question: How many spawn-threads can be opened in Arma 3? Is there any number or any maximum? I guess its depending on servers hardware is it?

I made a test for this. I'm not sure if it's hardware specific or not. For my computer, a lot. I opened >1000 threads all with a waitUntil inside, still didn't see performance loss.

Share this post


Link to post
Share on other sites

Hmm. I don't think it matters how many threads and waitUntils. I think it just matters what's in them.

Edited by Iceman77

Share this post


Link to post
Share on other sites
Hmm. I don't think it matters how many threads and waitUntils. I think it just matters what's in them.

This ^^^

1 thread of map wide nearestObject checks is way more than intensive than 1000 of _val = 1 + 1; hint str _val

Share this post


Link to post
Share on other sites
Hmm. I don't think it matters how many threads and waitUntils. I think it just matters what's in them.

Alright, thanks guys!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×