4

I come from the C++ world.

And recently, I started using Apache Thrift, the RPC framework. I am writing PHP client code and Python server code.

When I am reading the PHP implementation, I find the following:

class TStringFuncFactory {
private static $_instance;

/**
 * Get the Singleton instance of TStringFunc implementation that is
 * compatible with the current system's mbstring.func_overload settings.
 *
 * @return TStringFunc
 */
public static function create() {
    if(!self::$_instance) {
        self::_setInstance();
    }

    return self::$_instance;
}
....
}

It is the singleton WITHOUT locks.

Question

What is the processing pattern of PHP? Does it guarantee that this won't happen risk condition.

JensG
  • 13,148
  • 4
  • 45
  • 55
Alex
  • 1,737
  • 2
  • 20
  • 35
  • maybe `_setInstance` method handles any locking/access issues? – Nikos M. Jun 03 '15 at 07:51
  • @NikosM. I am not sure, that's why i ask here. There should be some locking mechanism to ensure its correctness. As I am quite new to php, so I want to know its details. – Alex Jun 03 '15 at 08:03
  • i dont use apache thrift, but cant you look into the source code for that and see your self, or post it here? – Nikos M. Jun 03 '15 at 08:04
  • @NikosM. you can refer to this https://github.com/apache/thrift/blob/master/lib/php/lib/Thrift/Factory/TStringFuncFactory.php, where `_setInstance` apparently does not use locks. – Alex Jun 03 '15 at 08:07
  • i see maybe its not needed, this is just a *shim* to handle multibyte strings (in case they are not part of the php instalation) and not related to multithreading/locking, so i understabnd, i might be wrong – Nikos M. Jun 03 '15 at 08:10
  • it also depends on the php version installed and whether it is thread safe or not (whether it shares state between php threads or handles any such issues, php itself and not the framework) – Nikos M. Jun 03 '15 at 08:12
  • Code that PHP developers write (userland code) is single threaded, shared-nothing. The PHP process itself might thread, to handle concurrent requests, but those threads share nothing between them. You can think of it as JavaScript execution engine, where everything happens in the same thread. Therefore, you don't need a lock since the object won't be shared with anything, which means that to get a singleton - check if a property contains the instance, if not create one, return the property (which is now an instance of an object). – N.B. Jun 03 '15 at 10:40
  • @N.B., correct but this depends on php version and build i.e php builds marked as "thread-safe". Else some care is needed even in userland code for some tasks which might tap into thread issues – Nikos M. Jun 03 '15 at 11:28
  • @NikosM. that's not true. Thread safe versions of PHP are able to respect exclusive locks on files and similar operations, and it's mainly aimed at how it interacts with underlying web server. They **don't share** objects that were created in PHP scripts, and code like OP posted is **completely safe and valid**. Now, just for you and your own exercise - try to demonstrate how the above code is "unsafe", and please - do show how you'd implement the lock to avoid race conditions. – N.B. Jun 03 '15 at 11:37
  • @N.B.php is not just for the web server, i think this is not the case, but i will not pursue this here since i need to look up specific codes which i havent seen for a long time (so i might also be wrong). having a small doubt about it (btw did not say the above code is unsafe) – Nikos M. Jun 03 '15 at 11:41

1 Answers1

1

+1 @N.B.

PHP (cli or http) exists as a single thread on a single cpu core unless you do some real work to make your application multithreaded.

How can one use multi threading in PHP applications

For the HTTP side, each PHP execution lives and dies with the request cycle. The server may process several requests concurrently, which would result in several concurrent PHP executions, but each is entirely independent from the next.

So basically this is a non-issue. For all intents and purposes checking the static data member satisfies the singleton pattern.

Community
  • 1
  • 1
Collin James
  • 9,062
  • 2
  • 28
  • 36