锁,这个词我们并不陌生,主要的应用场景会发生在高并发下进行锁。今天的这篇文章咱们主要来讲解一下swoole的锁的机制,swoole_lock是如何实现的。
swoole_lock类支持5种锁的类型:
- 文件锁 SWOOLE_FILELOCK
- 读写锁 SWOOLE_RWLOCK
- 信号量 SWOOLE_SEM
- 互斥锁 SWOOLE_MUTEX
- 自旋锁 SWOOLE_SPINLOCK
创建这些锁的过程其实就是调用构造函数的过程,调用的形式如下:
swoole_lock->__construct(int $type, [string $lockfile])
$type为锁的类型
$lockfile,当类型为SWOOLE_FILELOCK时必须传入,指定文件锁的路径
下面我们介绍下这个锁的实现
static"|ls","global","filelock","open","failed","htmlcode">int"htmlcode">int"htmlcode">int"semctl(SETVAL)"); "htmlcode">int"htmlcode">int swMutex_create(swLock *lock, int use_in_process) { int ret; bzero(lock, sizeof(swLock)); lock->type = SW_MUTEX; pthread_mutexattr_init(&lock->object.mutex.attr); if (use_in_process == 1) { pthread_mutexattr_setpshared(&lock->object.mutex.attr, PTHREAD_PROCESS_SHARED); } if ((ret = pthread_mutex_init(&lock->object.mutex._lock, &lock->object.mutex.attr)) < 0) { return SW_ERR; } lock->lock = swMutex_lock; lock->unlock = swMutex_unlock; lock->trylock = swMutex_trylock; lock->free = swMutex_free; return SW_OK; }免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com狼山资源网 Copyright www.pvsay.com暂无“swoole锁的机制代码实例讲解”评论...