ID:325937
 
I've been using the search function to look, but I have seen an incomplete version of what I want to implement.

I plan to have a ATB system where even while the player is looking for moves/idling, the NPC will be able to act regardless. Think chrono trigger/FF series.

Here's what I have in terms of design plan (following a mailbox scheme):

/**************************/
- Battle Main
Should initialize all other threads, handle graphics (including attack animation), and wait for the other threads to end for victory/defeat.

- Team
There will be 2 instances of this thread, 1 for the player team, and 1 for the enemy team. Here, they will simply idle until a time period passes (server_clock based). When it does, the enemy team will automatically chose a move and pass it onto the queue (discussed later). For the player, they can choose options. However, until the player thread commits to a particular move, it will not communicate with anything else.

- Mailbox/Data Tank
Every thread will be communicating through this obj. It will contain info on all the NPC/players (health, status, ...). In addition, there will be an action queue, where the two teams will pass there actions to this queue, and the battle main will pop from it to compute the action, in terms of graphics and data manipulation.

/*******************/

Now, here's the itch I need scratched:
I've seen a spawn command in the man pages, but never any sort of mutex/semaphores/locks. To avoid race conditions, is there any procedures or ways to ensure this?
DM isn't like C++, Java or any other language.
BYOND runs on a single thread. spawn() just creates virtual thread.
Threads without any delay (sleep()) will be terminated because of recursion limit or cause incredible lag. If you use sleep it'll execute your thread once per frame/tick or rarer.

If this make it easier, think of spawn() like about functor which has set time to execute. At the beginning of each frame DM loops through all active functors and checks which ones need to be executed.