Vapm
  • About
    • Why is it called unblocking?
    • Next update?
    • Credit
    • Info
  • System
    • runEventLoop()
    • runSingleEventLoop()
    • setTimeout()
    • setInterval()
    • fetch()
    • read()
    • time() & timeEnd()
  • Asynchronous
    • getId()
    • await()
  • Promise
    • c()
    • getId()
    • getFiber()
    • isJustGetResult()
    • getTimeOut()
    • getTimeStart()
    • getTimeEnd()
    • setTimeEnd()
    • canDrop()
    • getStatus()
    • getResult()
    • getReturn()
    • getCallback()
    • resolve() & reject()
    • then() & catch() & finally()
    • useCallbacks()
    • all()
    • allSettled()
    • any()
    • race()
    • Some common methods
  • CoroutineGen
    • runBlocking()
    • runNonBlocking()
    • Deferred
    • Deferred Await All
    • repeat() & delay()
    • Channel
      • Example about Channel
    • AwaitGroup
      • Example
    • Mutex
      • Example
    • Goroutine vs CoroutineGen
      • Concurrency
  • Thread
    • How do I create a thread?
    • How do I share data between threads?
    • Advanced Thread
  • Worker
    • Example
  • Stream
    • read()
    • write()
    • append()
    • delete()
    • create()
    • overWrite()
  • FAQ
    • Why System::runEventLoop() and System::runSingleEventLoop() ?
    • Do you want concurrency?
  • PocketMine-PMMP
Powered by GitBook
On this page
  1. Thread

How do I share data between threads?

Like the method of creating a Thread, you only need to depend on the Thread class and do nothing.

This is Thread

use vennv\vapm\Thread;

class TestThread extends Thread {

	public function onRun(): void {	
		$data = self::getSharedData();
		
		$data["I Love PHP"] = ["PHP", "GOOD"];
		
		self::postMainThread($data);
	}
}

This is Main Thread

use vennv\vapm\Thread;

class SharedData extends Thread {
	public function onRun(): void {}
}

SharedData::addShared('This is key Thread', ['data1', 'data2']); // you can use setShared() method

$class = new TestThread();
$class->start()->then(function($data) {
    var_dump(SharedData::getDataMainThread());
});

Output:

array(2) {
  ["This is key Thread"]=>
  array(2) {
    [0]=>
    string(5) "data1"
    [1]=>
    string(5) "data2"
  }
  ["I Love PHP"]=>
  array(2) {
    [0]=>
    string(3) "PHP"
    [1]=>
    string(4) "GOOD"
  }
}

You should not require the php file that is registered to the thread because that will cause process errors.

PreviousHow do I create a thread?NextAdvanced Thread

Last updated 9 months ago