2

By definition, true threads don't share memory, and therefore cannot share data.

I am looking for a way, however, to keep a running connection count (not just connections, a few other things) that can be updated from within any one of the threads in a server, and will be shared (static) across all threads. How would I do this?

A Few Notes:

  • Performance is key, so no databases, no external server connections, preferably no Memcached

  • This data is meant to be updated several thousand times a second

  • The data must, after being updated from one thread, be instantaneously available, with the new value, to all other threads

  • Ideally, the data should be simultaneously available to multiple threads

5
  • what's the big picture here? Commented Nov 12, 2013 at 2:59
  • Sequence numbers for outbound data, used for generating unique IDs Commented Nov 12, 2013 at 3:01
  • Sorry to disappoint, but if it helps, it was more of a test to see if I can do something much grander. Basically, I'm trying to put some Node.js type behavior in PHP. Commented Nov 12, 2013 at 3:05
  • I'm curious why no memcached. This is the kind of thing it was designed for. Commented Nov 12, 2013 at 3:06
  • Latency minimization. Memcached is fast, but it isn't instantaneous, at least not in the eyes of servers communicating with it. Commented Nov 12, 2013 at 3:07

1 Answer 1

2

Using Xcache, you can use the following API:

int   xcache_inc(string name [, int value [, int ttl]])

This will atomically increment the value identified by name and store it in shared memory that will live across requests. To retrieve the value from any of the running threads in Apache use:

mixed xcache_get(string name)

Note: When Apache (httpd) is restarted, these values are lost.

Note 2: By definition threads do share memory and easily share data given they are running within the same process. It is one of the primary reasons to use them when performance is critical and large amounts of data is processed in parallel and co-ordinated between threads rather than potentially slower IPC or complex shared-memory solutions. However, sharing data between threads is complicated, particularly when the data is mutable and should be undertaken by those with a thorough understanding of the challenges.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.