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

Multi-Threading

Comments in 'General Discussion' started by aliuly, Apr 7, 2015.

  1. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    Hi,

    Is there any interest in making PocketMine more threaded? (i.e. use more threads so that multi-core systems can be better used?)

    I know the current focus is on MCPE 0.11 and biomes + entity physics. However a big change like this is better done earlier rather than later.

    What is the general thinking about that?
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PocketMine 1.5 is going to have support/APIs/libraries in easier use off threading, according to @shoghicp.
    Samueljh1 likes this.
  3. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    What I was thinking is that maybe we should have one thread per player, and one thread per level. Data sharing is mostly done on a per level basis. (i.e. normally players interact with each other through the Level). On the other hand, a lot of plugins and anti-cheating is done on a per player basis (i.e. checking what a player does and execute something).
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That would be terrible. A lot of possible race conditions. And possibly memory issues because of having many threads.
    Moreover, pthreads itself is buggy in OOP. Doing that would lead to disastrous bugs.
  5. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    Not necessarily. if we start first by having one thread per level, that thread should manage all access to the level. Since this is done through a well defined API, then it should be no problem to encapsulate. It is very rare when an operation would need to access/manipulate data from multiple threads/levels. So each thread can easily run independently without sharing any data.

    Players could be done with worker threads. Normally a lot of the processing (specially done by plugins) would be manipulating player input (which is not shared with other threads) or reading state from the Level (which would be done through the API to get data from the Level thread). If for example you are doing anti-cheating code, then the input is cancelled (a packet is sent to the MCPE client) and nothing is changed on the level data.
    On the other hand, if the input is allowed, then it is modify operates are handed over th the Level thread to apply.

    Essentially, the idea would be to not share data, and let controllers running on threads do all changes.
  6. Goerick
    Offline

    Goerick Active Member

    Joined:
    Jan 24, 2014
    Posts:
    252
    Minecraft User:
    goerick
    So now I can make a full use of my AMD Opteron? :3
  7. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    Talking about something does not mean coding it...
  8. sekjun9878
    Offline

    sekjun9878 Staff Member PocketMine Team

    Joined:
    Aug 22, 2013
    Posts:
    108
    Plugins:
    1
    Minecraft User:
    sekjun9878
    Let's not have another API major version bump so soon after the havoc that was transitioning from 1.3 to 1.4 xD
    PEMapModder likes this.
  9. shoghicp
    Offline

    shoghicp Staff Member PocketMine Team

    Joined:
    Aug 22, 2013
    Posts:
    436
    Plugins:
    14
    Minecraft User:
    shoghicp
    I would love to make PocketMine work on a thread per level! But plugin developers would hate it, and it would break backwards compatibility (worst thing I did since API 1.0.0 was deprecating an Event and do not fire it).

    PocketMine 1.5 has lots of threading improvements, for plugin developers and normal usage.
    World generation, chunk population, and network compression will use the Async workers (so increase this if you want to use more of your resources!). Threads have a proper class loader that is shared across all of them (no need to manually include files on threads).

    So, in 1.4, this was the list of threads:
    • Main thread
    • Console I/O (mostly idle)
    • RakLib thread
    • Logger thread (mostly idle)
    • Generation thread (one for all worlds)
    • Async Workers [one or more] (used for AsyncTasks, mostly plugins)
    In 1.5, however, it changes completely:
    • Main thread
    • Console I/O (mostly idle)
    • RakLib thread
    • Logger thread (mostly idle)
    • Async Workers [one or more] (Now with shared class loader, and per-thread data saving)
      • Generation tasks
      • Population tasks
      • Compression tasks
      • Plugin tasks
    The only overhead now is chunk serialization, which is needed by phreads.

    Also, more threads != more performance always.
    hoyinm14mc, EvolSoft, Goerick and 5 others like this.
  10. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    I think running too many threads is a very bad practice.
    PHP is already a slower language than others.
    Then you have to consider also the max number of threads for task supported by OS and CPU
    hoyinm14mc likes this.
  11. shoghicp
    Offline

    shoghicp Staff Member PocketMine Team

    Joined:
    Aug 22, 2013
    Posts:
    436
    Plugins:
    14
    Minecraft User:
    shoghicp
    Yep! Lots of people don't understand threads and just set the async worker count to 1000 or something bigger ("It'll be faster, and I have a XXX Ultra Pro PC") then they blame me because they hit the OS thread limit per process.

    And what I've done is run more on less threads. They async workers will get reused for everything, so they are well used if set to a proper count.
    EvolSoft likes this.

Share This Page

Advertisement