PHP 中的 C# 锁定语句等效于什么?
为了并发性和确保数据的完整性,如何获得给定对象的互斥锁?您是否需要在数据库或文件中使用锁定,或者 PHP 是否支持类似的操作?
For concurrency and ensuring the integrity of the data, how would you obtain a mutual-exclusion lock for a given object? Would you need to use locking within the database, or a file, or does PHP support something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
PHP 不支持多线程,因此没有对象的锁定机制。如果你想锁定一个文件,你可以使用 flock 来实现。无需锁定数据库,因为数据库引擎通常可以处理多个连接。
PHP doesn't support multithreading so there's no locking mechanism for objects. If you want to lock a file you could use flock for that. There's no need to lock database as database engines usually can handle multiple connections.
请记住,PHP 不是多线程的,因此您不太可能需要这样的东西...但是,如果您使用共享内存或任何其他外部资源,则可能需要。在这种情况下,请使用 smaphores:
http://www.php.net/ Manual/en/function.sem-acquire.php
http ://www.php.net/manual/en/function.sem-get.php
http://www.php.net/manual/en/function.sem-release.php
Bare in mind PHP is not multithreaded, so it's unlikely you need anything like this... however, may be needed if you use shared memory, or any other external resources. In such case use smaphores:
http://www.php.net/manual/en/function.sem-acquire.php
http://www.php.net/manual/en/function.sem-get.php
http://www.php.net/manual/en/function.sem-release.php
蜂拥而至的文件。
如果您想在数据库中使用锁定,那么您需要使用这些数据库的锁定功能。几乎所有数据库都使用某种形式的锁机制。
没有对象
flock for files.
If you are wanting to use lock in the database, then you would need to use the lock features for those databases. Almost all databases use some form of lock mechanism.
nothing for objects
它有信号量支持
它有羊群
http://www.php.net/manual/en/function.flock。 php
你可以在 MySQL 中进行表锁定。
It has semaphore support
It has flock
http://www.php.net/manual/en/function.flock.php
You can do table locking in MySQL.
就像其他人回答的那样,由于 PHP 不是多线程的,因此您不需要锁定对象。但是,如果您需要锁定数据库,您可能需要查看事务。有许多关于使用 PHP 和 MySQL 进行事务的教程(可能也适用于其他 RMDBS)。
Like others have answered, since PHP is not multithreaded you do not need to lock on objects. However, if you need to lock on the database you might want to look to transactions. There are many tutorials for doing transactions with PHP and MySQL (and probably for other RMDBS as well).
PHP可以在多线程环境中运行。
即使 Web 服务器不使用多线程,也可以同时运行多个进程。
在这种情况下,并发问题仍然可能发生。
如果你想要类似锁的东西来解决并发问题,你可以使用信号量:
http://www.php.net/manual/en/function。 sem-acquire.php
PHP can run in multi-threaded environments.
Also multiple simultaneous processes can be running, even if the web server is not using multiple threads.
In this case, concurrency problems can still happen.
If you want something similar to lock to solve concurrency problems, you can use semaphores:
http://www.php.net/manual/en/function.sem-acquire.php