Jump to content
Sign in to follow this  
zapat

Question about threads (spawn)

Recommended Posts

In http://community.bistudio.com/wiki/Code_Optimisation I read the following about the 0.3ms delay:

...In this way you should refrain from creating to many threads in your code to stop the system from scaling to the point functionality is seriously degraded....

...Obviously this problem is only an issue when your instances are lasting for longer than their execution time, ie spawned loops with sleeps that never end, or last a long time...

I would run this code on many objects: I want to check stuff, but only once in a while is enough. Originally I thought: if I'm checking only once in a minute, that will lift any stress from the system, so basically I can run the checks on like hundreds of objects.

spawn 
{
  while {alive _object} do
  {
    check stuff;
    sleep 60; //<-I am talking about this
  };
};

Having read the above I am confused: I thought that spawned (parallel) instances are left alone while sleeping, while active commands taking precedence.

But if I understand the article well: sleeps are waited until finished.

Could some shed some light on how this really work?

What is good plan to use for my goal?

Edited by zapat

Share this post


Link to post
Share on other sites

From my experience, using spawn has always been a more effective way of doing things than actually calling a new script.

Share this post


Link to post
Share on other sites

Horner: yea, that's correct, but my question wasn't about that. Thanks though.

I made a little test, which shows that the above is not true. Or it is me, who misunderstood something.

for "_threadNo" from 0 to 1000 do
{
   [_threadNo]spawn
   {
       _threadNo = _this select 0;
       while {true} do
       {
           _s = random 100;
           sleep _s;

           player sidechat format ["%1  - %2",_s, _threadNo];
       };    
   };
};

Which little script creates 1000 threads: those produce their sidechats as expected. The combat happening meanwhile doesn't suffer any FPS loss.

So why is this an issue if instances are lasting for longer than their execution time?

Share this post


Link to post
Share on other sites

Maybe try something other than just printing a number, have it compare your players weapons to an array of 30 classnames and check config values to see if it's a pistol or check near entities knownabout level towards the player in each loop or something? That might show the slowdown better?

Share this post


Link to post
Share on other sites

Yes, but it would slow the system down because like 30000 check would run at the same time, while the article was talking about spawned loops with sleeps that... last a long time.

My question was born, because I use a lot, a LOT of spawned loops with sleeps that last a long time, and the article warned to avoid that.

It must be my misunderstanding then. Happens sometimes. :)

Edited by zapat

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  

×