dongqiao1151 2015-11-04 10:32
浏览 69

php pthreads并使用类来存储线程之间共享的数据

I'm fairly new to PHP, and now very new to pthreads.

I'm using the latest PHP7 RC6 build, with pthreads built from git/src to get the latest (and tried the 'official' v3.0.8 one), on Ubuntu 3.13.0-66-generic

I'm trying to write a threaded solution to read in data from a socket and process it. I'm using threading to try to maximize my performance, mainly due to the fact I'm doing operations like http requests (to AWS DynamoDB and other services) and such that are waiting for responses from external systems, and therefore I can benefit from threading.

The real code I have is more complicated than this is. This is a simple example to show my problem.

What I am trying to do is to 'cache' certain information in an 'array' that I get from a database (AWS DynamoDB) so that I can get better performance. I need each thread to be able to use/access and modify this 'global' cache, and with multiple 'records' in the cache.

I had great success with testing and simply storing a string in this way, but now I'm doing it for real, I need to store more complicated data, and I decided to use a little class (cacheRecord) for each record, instead of a simple string of data. But the problem is that when I try to assign a value back to a class member, it seems to not want to 'save', back to the array.

I managed to get it to work by copying the whole 'class' to a tmp variable, modifying that, and then saving back the whole class to the array, but that seems like an overhead of code, and also I would need to wrap it in a ->synchronized to keep integrity between threads.

Is this the only way to do it correctly, with copying it to a tmp and copying it back and using 'synchronized', or am I doing something else wrong/stupid?

Experimenting with it, I made the cacheRecord class 'extends Threaded'. This made the single assign of the member work fine, but this then made it immutable, and I couldn't unset/delete that record in the cache later.

Code to show what I mean:

<?php

class cacheRecord {
    public $currentPos;
    public $currentRoom;
    public $someOtherData;
}

class cache extends Threaded {
    public function run() {}
}

class socketThread extends Thread {

    public function __construct($myCache) {
        $this->cacheData = $myCache;
    }

    public function run() {

        // This will be in a loop, waiting for sockets, and then responding to them, indefinitely.

        // At some point, add a record to the cache
        $c = new cacheRecord;
        $c->currentPos = '1,2,4';
        $c->currentRoom = '2';
        $this->cacheData['record1'] = $c;

        var_dump($this);

        // Later on, update the cache record, but this doesnt work
        $this->cacheData['record1']->currentRoom = '3';

        var_dump($this);

        // However this does work, but is this the correct way? Seems like more code to execute, than a simple assign, and obviously, I would need to use synchronized to keep integrity, which would further slow it down.

        $tmp = $this->cacheData['record1'];
        $tmp->currentRoom = '3';
        $this->cacheData['record1'] = $tmp;

        var_dump($this);

        // Later on some more, remove the record
        unset($this->cacheData['record1']);

        var_dump($this);

        // Also will be using ->synchronized to enforce integrity of certain other operations
        // Just an example of how I might use it

/*
        $this->cacheData->synchronized(function() {
            if ($this->cacheData['record1']->currentRoom == '3') {
                $this->cacheData['record1']->Pos = '0,0,0'; // Obviously this wont work as above.
                $this->cacheData['record1']->currentRoom = '4';
            }
        });
*/
    }
}

// Main

$myCache = new cache;

for ($th=0;$th<1;$th++) { // Just 1 thread for testing
    $socketThreads[$th] = new socketThread($myCache);
    $socketThreads[$th]->start();
}
  • 写回答

1条回答 默认 最新

  • dongtanjian9310 2015-11-13 17:27
    关注

    extends \Threaded is the way to go. However, "anything" in the cache should be extended from this, not only the cache itsef.

    It is explained somewhere in the manuals (sorry dont remember exactly where) than only volatile (aka threaded) object will not me immutable.

    So if your class cacheRecord is not extended from threaded, it will be immutable, even into another threaded structure.

    threaded makes inner attributes array automatically volatile (so thread-usable), but not object if they are not extended from threaded.

    Try extending cacheRecord from threaded and tell me if it works.

    • Phil+
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度