swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.44k stars 3.16k forks source link

Swoole Table locking after Swoole Lock removal #2711

Closed gcmartin closed 5 years ago

gcmartin commented 5 years ago

Hello,

With the coming removal of Swoole Lock in v4.5, what is the new way to handle locking with Swoole Table in multi-processes/threads environment? The current document states:

In order to keep the data synchronization, have to use Swoole\Lock if the memory is modified and accessed by multiple threads or processes.

Thanks!

doubaokun commented 5 years ago

@gcmartin New programming model coroutine became the mainstream, people are able to build concurrent logics easily within a single process with coroutine. Any reason why you like to sync between multiple processes?

gcmartin commented 5 years ago

To share data between Swoole Websocket Server workers, for instance to cache data and to store/update user-related information faster than relying on external in-memory databases like Memcached/Redis.

DavidPesta commented 5 years ago

I was just about to use the Swoole lock. :(

Why is it being removed?

How do you find that sort of news?

matyhtf commented 5 years ago

Deadlocks can occur when using locks in coroutines

exam

$lock = new Swoole\Lock();
$c = 2;

while ($c--) {
    go(function () use ($lock) {
        $lock->lock();
        Co::sleep(1);
        $lock->unlock();
    });
}
matyhtf commented 5 years ago

Deadlocks can occur when using locks in coroutines

exam

$lock = new Swoole\Lock();
$c = 2;

while ($c--) {
    go(function () use ($lock) {
        $lock->lock();
        Co::sleep(1);
        $lock->unlock();
    });
}
matyhtf commented 5 years ago

We are rethinking whether to maintain the module of the lock. It may not be removed.

Thank you for your feedback