GithubHelp home page GithubHelp logo

Volatile isn't thread safe about pthreads HOT 7 OPEN

joeyhub avatar joeyhub commented on August 15, 2024
Volatile isn't thread safe

from pthreads.

Comments (7)

joeyhub avatar joeyhub commented on August 15, 2024

Double bufferring appears to avoid the issue:

private $data = [[], []];
private $i = 0;

public function push($value):void {
    $this->data[$this->i][] = $value;
}

public function drain():Volatile {
    $data = $this->data[$this->i];
    $this->data[$this->i] = new Volatile();
    $this->i = +!$this->i;

    return $data;
}

public function shouldWait():bool {
    return count($this->data[$this->i]) === 0;
}

In some cases it's even weird, an array gets misplaced in the volatile. Someone is definitely screwing around with reused pointers there.

from pthreads.

sirsnyder avatar sirsnyder commented on August 15, 2024

Could you provide a runnable example? Thx!

from pthreads.

joeyhub avatar joeyhub commented on August 15, 2024

Here: https://github.com/joeyhub/aphpsync/blob/master/prototypes/test.php

from pthreads.

joeyhub avatar joeyhub commented on August 15, 2024

It's worth pointing out that I have a ref issue there. Eventually a command is sent to a thread which holds a reference up to a thread to notify when done. Nothing references the child threads, instead everything references the man in the middle.

I've been meaning to fix that later for graceful completion. I didn't really consider it related as I would expect it to hang on wait and it's been doing something weird hanging somewhere else. Though at first I didn't consider them related and would really expect a segfault, hang on wait or error, there's a good chance they are related as volatile's weirdness seems to set in a around about where the refs are gone and also if the handling of lost ref (hanging in weird places) is broken somehow then it wouldn't be surprising if it has other problems.

Still a bit of a headscratcher that double bufferring improves it (should remove any possibility for concurrent access in this particular use case) though I think it just reduces it.

from pthreads.

joeyhub avatar joeyhub commented on August 15, 2024

Adding graceful (though leaky) thread shutdown does appear to improve a lot of secondary issues I've been encountering with volatile even after the double bufferring.

However I'm not sure how reliable that graceful shutdown is. If the main thread dies it looks as though things will probably just hang. Similarly I practically need a thread monitor to potentially handle dead threads (try catch isn't reliable enough in my book either). I guess if thread death notified perhaps that could be enough though kind of ugly.

Worse still is tuning up the concurrency (workload per thread) and I got it to segfault. Whatever it is it's about one in 16000.

from pthreads.

 avatar commented on August 15, 2024

Honestly I'd just do while($this->data->shift()); for draining.

from pthreads.

joeyhub avatar joeyhub commented on August 15, 2024

I've considered that although it's not really documented. Even then things shouldn't be hanging and segfaulting as they are. The current release definitely isn't stable or trustable.

Also relying on type juggling there isn't necessarily safe unless you always wrap the data in something that juggles to true. Similarly it doesn't seem to perform well that I would have to iterate if access to that needs to be synchronised.

Looking at the source those operations are only valid for Volatile anyway. Chunk actually deletes apparently so should probably be faster. Shifting each one isn't going to be all that efficient. Chunk should at least for example only need to lock once.

from pthreads.

Related Issues (20)

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.