Is PHP thread-safe?

56

Solution 1

I know gettext and set_locale is not threadsafe. PHP should not be used with a threaded MPM.

PHP Isn't Thread-Safe Yet.
Running PHP not threaded.

Solution 2

See Where can I get libraries needed to compile some of the optional PHP extensions? for a list of thread-safe and nonthread-safe extensions (* marked are not thread-safe and others are).

Solution 3

A better question might be, "Is the following PHP code going to trigger access violations if MPM is used?" Or, "Have you experienced odd behavior likely attributed to concurrency issues using the following functions?"

Otherwise, it's Russian roulette. If you're using some packaged application, it may work just fine now but break a month from now when a new version of the application comes out.

I strongly advise against using MPM with PHP in general. However, if you have some small code to run, you could post it, and we could tell you if you're going to hit a pitfall.

Share:
56
Andreas Björksved
Author by

Andreas Björksved

Updated on July 09, 2022

Comments

  • Andreas Björksved
    Andreas Björksved almost 2 years

    How can i use the content of switches.address as a table-name, and switches.pin as a column-name to perform some sort of joined query on "switches" that that gives me (in the below case) the value of PIN3 from 0x68?

    something like

    SELECT name, state FROM switches
    

    with 0x68.PIN3 as state

    CREATE TABLE switches (
    name varchar(20), address varchar(20), pin varchar(20));
    
    INSERT INTO switches VALUES
    ("Lights_Kitchen", "0x68", "PIN3");
    
    |      name      | address | pin  |
    +----------------+---------+------+
    | Lights_Kitchen |  0x68   | PIN3 |
    
    
    CREATE TABLE `0x68` (
    PIN1 INT, PIN2 INT, PIN3 INT, PIN4 INT);
    
    INSERT INTO `0x68` VALUES 
    (0,0,1,0)
    
    | PIN1 | PIN2 | PIN3 | PIN4 |
    +------+------+------+------+
    |  0   |  0   |  1   |  0   |
    

    and so on..

    • StanGeo
      StanGeo almost 3 years
      Hi, Can you please reformat your code. I am unable to understand the problem here. Please add some Table data as demo. You can check some other questions on this portal for example formattings. (e.g. stackoverflow.com/questions/57011198/…)
    • astentx
      astentx almost 3 years
      Please describe formal algorithm in terms of tables and fields, not a value X from table Y. Why? Because to get the value X from table Y you just need to select distinct col from Y where col = X. Other manipulations are attempts to invent the way you need it
    • Barmar
      Barmar almost 3 years
      You need to generate dynamic SQL in a stored procedure. Tables and column names can't be generated dynamically in a regular query. It's usually a bad design to require this.
    • Tangentially Perpendicular
      Tangentially Perpendicular almost 3 years
      This is a really bad idea. You could just create a table with the address as a column, and your query would then contain a clause like WHERE address = '0x68'. You might do something similar for pins, but actually, you should explain what the root problem is here, rather than attacking some small part of it. It will get you better answers. See also XY Problem
    • Strawberry
      Strawberry almost 3 years
      Seriously consider normalising your schema design
    • Andreas Björksved
      Andreas Björksved almost 3 years
      We have a database called hardware that contains tables named by the i2c address that they represent. Some hardware returns FLOAT(adc converters) and some returns INT (gpio) and they all have different amount of pins, therefor i can't use a table design like the one @TangentiallyPerpendicular proposed. I need help formulating something like a dynamic sql procedure or maybe a function, but it's way over my level