PHP Pthread Socket Gets the object value in the global static object

Source: Internet
Author: User
I want to ask PHP pthread usage

I'm working on a poker game. Client and server are all written.

Now you can play cards more than a person can do, but the socket is a single thread

When someone doesn't play cards, they have to pause.
It's a lot of sleep anyway.
Just sleep, please.
The entire server is stuck because it is single-process single-threaded.
So to solve this problem, I'm going to use Pthread. A player is a thread that is the player object that holds the thread and facilitates communication and delay.
I used the thread to run it as if I could not take the data with the main process or could a player object be a thread
Because I didn't use it too much. It's not clear how to implement the thread, and I don't know if I can make it.

Please give a thinking card master for a week, have been developed and can not change the language and then the server is windows because the code is too simple to paste the code yourself * * Asterisk cannot get
Then there is a good idea to tell you that sleep pauses with the communication part .....
Class waittingroom{
public static $Rooms = Array ();
public static $Gamers = Array ();
Public Function Addgamer ($gamer _id) {
DB Query
Self:: $Gamers = new Gamer ($gamer _id);
}
}
Waittingroom::addgamer ("game_id");

Class Connection extends Thread {
Public $connection;
public $uid = null;
Public $is _free;
Public function __construct ($client) {
$this->is_free = false;
$this->connection = $client;
$this->start ();
}

Connections = Array ();
do{
If there's a connection.
connections [] = new Con ($sockit);
}while

Class Con () {
Public function __construct ($client) {
$this->connection = $client;
$this->start ();
}
Public run () {
while ($this-connection read data) {
Then the data is processed here
Drop global static variables when processing data
Waittingroom:: $Gamers [Identify]-> specified function or variable cannot get data db operation is not ********

}

}
}


Reply to discussion (solution)

You seem to have a problem with your thinking, right? Or maybe I'm a daoxing. I can't understand how your multithreading works.
A socket server can only listen to one port, you use multi-threaded how to set the listening port? How does the user know that the port is free?

You said you used a lot of sleep, why?
Since there is a loop waiting (I guess), then sleep will slow down the process

If I guess unfortunately, then you're using a point-to-point socket architecture
You should use the Select schema (Point-to-multipoint) as you describe the application:
To create a master listening master
Add it to the connection word list sockets[] = Master
In a perpetual loop (saying dead loops are not good to hear):
Get a copy of the connection word list changed = Sockets
Read each connector state socket_select (changed) (because Socket_select changes the contents of changed, the previous step is to take a copy)
Socket_select only returns when the list of connections is changed, so there is no need for sleep to wait
Iterate through the list of valid connection words foreach (changed as socket):
if (socket = = Master) usually indicates a new connection request, joins the socket to sockets and responds
else indicates that a user has data incoming, read, and answered

As you can see, this architecture is not as complex as you think.

Understand that the part that you said has been implemented the socket receiving data is not a problem multiple users are also able to accept data to the data and use that data to create objects that are in the read and write operation of the server page created Object Hall object room object Player object lobby store player and room object when to play the game When you enter the room and place the player object in the room object and then the player is aligned and the game is created, the result is the game engine.
Licensing event synchronization and other issues have to wait 1 two seconds inside there is a computer .... The computer does not give sleep words can not see the licensing directly on your turn to the card.

Alas said a lot of their language level is limited ... is a Web page, a user a page. However, multiple users read and write in a single page.
A page to sleep directly into all the players and the player in the room can not receive the data so the multi-threading
Now the main thing is not too many threads. Extra threads sleep does not affect the main thread. Other users can read and write data
And now when the code is posted: Encountered a line thread cannot read in the global object .... Created object ... This is the player object.

No way?

If you need to use multi-threading,
if (socket = = Master) usually indicates a new connection request, starting a thread here, completing the socket join sockets, and answering
else indicates that a user has data passed in, where a thread is started, and the read is completed and answered

The main thread is responsible for scheduling, the child thread is responsible for operation

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.