Giter VIP home page Giter VIP logo

appserver-io-php.fhreads's Introduction

fhreads

A PHP extension to get real system threading functionality in php userland.

Build Status

What is it?

The extension gives you access to plain system threading functions for being able to implement threading utilites as part of the language directly in the PHP userland e.g. an abstract Thread class.

How to use?

This extension is in early-stage development. Please use with caution.

This is a simple example how to use a shared data object as normal php object with a thread safe counter property.

<?php

require_once __DIR__ . DIRECTORY_SEPARATOR . "Thread.php";

// define counter thread class which counts the shared data objects counter property
class CounterThread extends Thread
{
    /**
     * Constructor which refs the shared data object to this
     * 
     * @param object $data
     */
    public function __construct($data)
    {
        $this->data = $data;
    }
    
    public function run()
    {
        fhread_mutex_lock($this->data->mutex);
        ++$this->data->counter;
        $this->data->{$this->getThreadId()} = $this->getThreadId();
        fhread_mutex_unlock($this->data->mutex);
    }
}

$data = new \stdClass();
$data->mutex = fhread_mutex_init();
$data->counter = 0;

$ths = array();
$tMax = 10;

// initiate threads
for ($i = 1; $i <= $tMax; $i++) {
    $ths[$i] = new CounterThread($data);
}
// start threads
for ($i = 1; $i <= $tMax; $i++) {
    $ths[$i]->start();
}
// wait for all thread to be finished by joining them
for ($i = 1; $i <= $tMax; $i++) {
    $ths[$i]->join();
}

// echo status
echo "$tMax threads finished..." . PHP_EOL;
// dump shared data object
var_dump($data);
// echo status
echo PHP_EOL . "finished script!" . PHP_EOL;

This should give us an amazing output! :)

10 threads finished...
object(stdClass)#1 (12) {
  ["mutex"]=>
  int(19463520)
  ["counter"]=>
  int(10)
  ["139792551667456"]=>
  int(139792551667456)
  ["139792533702400"]=>
  int(139792533702400)
  ["139792506754816"]=>
  int(139792506754816)
  ["139792488789760"]=>
  int(139792488789760)
  ["139792515737344"]=>
  int(139792515737344)
  ["139792524719872"]=>
  int(139792524719872)
  ["139792479807232"]=>
  int(139792479807232)
  ["139792560649984"]=>
  int(139792560649984)
  ["139792542684928"]=>
  int(139792542684928)
  ["139792497772288"]=>
  int(139792497772288)
}

finished script!

See examples folder for more usage demonstration.

More documentation and examples comming...

appserver-io-php.fhreads's People

Contributors

zelgerj avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.