Jump to content

Thread safe way to remove items from a table


Igneous01

Recommended Posts

I'm working on a few scripts where when a client triggers a radio action, some code loops through a collection and gets the nearest objects to the player then does some logic on them. At the same time I want to be able to manage the collection and delete/remove any objects that are dead or nil. Is there a thread safe way of doing this so that the clients code doesn't have a race condition with the scheduled function cleaning up the collection?

 

There's not much information on this, but I assume timer.scheduleFunction() and triggering a function through MOOSE:MENU_GROUP_COMMAND are called on separate threads (please correct me if im mistaken) - if this is the case, are there any locks / atomics available to use within a mission script to synchronize the read/delete?

Developer of Kaukasus Insurgency - a customizable Dynamic PvE Campaign with cloud hosting and stats tracking. (Alpha)

 

http://kaukasusinsurgency.com/

Link to comment
Share on other sites

I have run into problems when removing elements from table inside event handler. The way I fixed it was to build a custom scheduler that runs tasks in series and then make the element removal in a function that is scheduled to execute in series. No more mysterious impossible situations during table iteration.

DCS Finland: Suomalainen DCS yhteisö -- Finnish DCS community

--------------------------------------------------

SF Squadron

Link to comment
Share on other sites

I once had code using co-routines and those go horribly wrong... In many ways. Simply forget using co-routines and scheduled calls. Normally you should be able to remove entries in a table. Scheduled calls are in the same thread. Only using co-routines a multi threaded environment can be setup but forget it to make it work in dcs... There problem is that co-routines aren't pre-emptive and neither are event driven. That means you need to write yourself a thread routine scheduler for these routines and asynchronous processing can only be achieved using scheduled calls. You can't call a co-routine and let dcs do something else in the meanwhile. The old code i wrote is commented out in the SET_BASE class... But when using co-routines, the scheduled co-routine calls would crash after garbage collection.

[TABLE][sIGPIC][/sIGPIC]|

[/TABLE]

Link to comment
Share on other sites

Hmm, so looks like best option right now is to use a hash table and set indexes to nil (which should be safer than resizing entire table while in scheduled call).

 

I don't think resizing an entire table in a scheduled function would bring much problems...

What errors did you get?

[TABLE][sIGPIC][/sIGPIC]|

[/TABLE]

Link to comment
Share on other sites

I don't think resizing an entire table in a scheduled function would bring much problems...

What errors did you get?

 

It's not so much errors, but my experience when trying to iterate and remove items from a collection with threads in other languages like c++ or c#. You wouldn't necessarily get an error, but your data might be corrupted if say index 12 was being updated with new data, but index 12 was deleted at same time, now the updates are partially applies to the next item that is in index 12 because table was resized.

Developer of Kaukasus Insurgency - a customizable Dynamic PvE Campaign with cloud hosting and stats tracking. (Alpha)

 

http://kaukasusinsurgency.com/

Link to comment
Share on other sites

Jump into your slack seat. I am online. You still know you slack Id and password, no? Come online and lets discuss.

[TABLE][sIGPIC][/sIGPIC]|

[/TABLE]

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...