Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Thread Safety

Comments in 'Plugin Development' started by PEMapModder, Apr 18, 2015.

  1. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
  2. sekjun9878
    Offline

    sekjun9878 Staff Member PocketMine Team

    Joined:
    Aug 22, 2013
    Posts:
    108
    Plugins:
    1
    Minecraft User:
    sekjun9878
    Personally, I find that keeping the constructor to an absolute bare minimum works well to keep in line with pthreads (where you can't even create new objects in the constructor. ty Zend ref tracker!). So I try to avoid creating anything that the thread will use in the constructor (such as sockets).

    So my constructor for a WIP plugin of mine is like this:
    https://gist.githubusercontent.com/...b8a2f9576b1270dfda9bec57daf9b7d/gistfile1.txt

    Which is just allocating variables to the class-context(dependency injection). The socket would be created inside the run() method to reduce bugs with resources being shared.

    Also note that you should avoid using Exceptions. If you use exceptions within pthreads, you will not get error messages nor will the thread be marked as isTerminated(). You can use "exit;" instead for the time being to mark it as terminated (and print the stacktrace manually). This only happens with custom exceptions - PHP core exceptions such as bad function name etc. doesn't seem to cause this bug.

    And also I'm not sure if you've tested your plugin, but pthreads by default does not import autoloaders. Which is why in the above code, the first thing I pass as a parameter is the \ClassLoader. You will need to import it yourself and then call $this->autoloader->register().
    I feel like this would error out if you didn't load the autoloader: https://github.com/PEMapModder/Smal...emapmodder/ircbridge/bridge/IRCServer.php#L41 but I haven't really tested this. Note that in PM 1.5, the autoloader is provided by default and you can run it with $this->registerClassLoader().

    Just my 2 cents!

    Edit: If anyone else has anything to comment, please feel free!
    Last edited: Apr 18, 2015
    Falk, iJoshuaHD and PEMapModder like this.

Share This Page

Advertisement