Jump to content
UBot Underground

[Howto] Make A Bot Thread Safe When Multithreading


Recommended Posts

Threading is always new and exciting. Your bots become quicker, they can process more data in a shorter time and they can handle more things at once. This has truly increased the power with which we can design and improve software. But as always great power comes with great responsibility.

 

The main problem with threading is that multiple threads would like to write or read from the same resources at once. Resources in this case would be a #variable, %list, or a &table. But if two threads write to the same resource at the same time the resources will become corrupted or in the worst case scenario your apps will crash.

This however can be easily solved with thread protection in this case the "Lock" and the "System Wide Lock" command that comes with the Communication Plugin. Locks protect a part of code being accessed by two or more threads at the same time, so that we will not experience corrupted data or the bot crashes.

 

http://i.imgur.com/VzFN2dF.png?1

 

All the commands inside the lock will be protected from being run simultaneously and there for will be thread safe.

http://i.imgur.com/ACTbvT9.png?1

 

Here we see a define that is not thread safe, if accessed by two threads at a time the data will become corrupted and/or the bot will crash. So how do we make this thread safe?

Below we can see that "append to file" is inside a thread container and is thus thread safe.

http://i.imgur.com/EMOgBkc.png?1

The reason it's thread safe is because only one thread at a time is allowed to access the code inside the container and all other have to wait untill the current thread is finished. The waitng will most of the time just take a few miliseconds but it insures us the resources will be protected from corruption.

The same method can be used for a #variable, %list, or a &table to ensure you don't have strange behavior where data becomes missing and things sync out of place.

 

http://i.imgur.com/WNisa6M.png?1

The lock name is used to identify the lock and the same lock name can be used throughout the code. So as soon as another lock with the same name gets accessed all other locks will also close and open if the thread has done it's work inside the lock.

 

The lock will only exist inside the bot itself, all other application won't be aware of the lock unless you use "system wide lock".

http://i.imgur.com/uMD1tXA.png?1

 

The "system wide lock" can be shared between bots. For example to bots can in this way write to the same file safely without the possibility that the data becomes corrupted. But be aware the lock names used must be identical in both bots!
 

Plugin required: Communication Plugin

Edited by diskwizz
  • Like 3
Link to post
Share on other sites
  • 8 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...