PHPackages                             mouf/utils.common.lock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mouf/utils.common.lock

ActiveMouf-library[Utility &amp; Helpers](/categories/utility)

mouf/utils.common.lock
======================

A simple package that provides functions to get a lock. Typically, you want to use locks when you want to be sure that 2 actions do not happen at the same time. For instance, if you regularly schedule cron tasks, you might want to be sure the last cron task finished before running the new one. A lock can help you do that.

v1.0.0(11y ago)167.5k1MITPHPPHP &gt;=5.3.0

Since Sep 15Pushed 10y ago12 watchersCompare

[ Source](https://github.com/thecodingmachine/utils.common.lock)[ Packagist](https://packagist.org/packages/mouf/utils.common.lock)[ Docs](http://mouf-php.com/packages/mouf/utils.common.lock)[ RSS](/packages/mouf-utilscommonlock/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (1)DependenciesVersions (2)Used By (1)

Lock Manager
============

[](#lock-manager)

The LockManager package is a simple package that provides functions to get a lock. Typically, you want to use locks when you want to be sure that 2 actions do not happen at the same time. For instance, if you regularly schedule cron tasks, you might want to be sure the last cron task is finished before running a new one. A lock can help you do that.

The Lock class
--------------

[](#the-lock-class)

The Lock class represents a lock. You can **acquire** a lock to lock the resource and **release** the lock to set it free. Of course, if the lock has been already acquired by another user, you won't be able to acquire it yourself. You can optionnaly wait for the lock to be freed by the other process to acquire it yourself.

Internally, locks are acquired by putting a lock on a "file" that is hidden in the temp directory. When creating a `Lock` instance in Mouf, you will therefore have to find a unique temp name for that file.

If your PHP script crashes or exits without explicitly releasing the lock, the lock will be automatically released, so that other processes can use the lock.

Example
-------

[](#example)

A first example: trying to acquire a lock without waiting

```
// Let's create the lock instance
$lock = new Lock("my_lock_name");

// Try to acquire lock without waiting
try {
	$lock->acquireLock();
	// ... Do some stuff ...
	$lock->releaseLock();
} catch (LockException $e) {
	// The lock could not be acquired... Let's ignore this.
}
```

A second example: acquire a lock and wait if the lock is not available

```
// Let's create the lock instance
$lock = new Lock("my_lock_name");

// Try to acquire lock and wait if the lock is not available
$lock->acquireLock(true);
// ... Do some stuff ...
$lock->releaseLock();
```

Good practices
--------------

[](#good-practices)

A good practice is to create the lock object via a dependency injection mechanism. This way, you can share instances of your lock accross services that require it. The [Mouf framework](http://mouf-php.com) let's you do that. Let's assume you create a "myLock" instance:

[![mouf instance](doc/images/instance.png)](doc/images/instance.png)

Your code to use the lock would look this:

```
// You need to create an instance "myLock" of the Lock class in Mouf first.
$lock = Mouf::getMyLock();

// Try to acquire lock without waiting
try {
	$lock->acquireLock();
	// ... Do some stuff ...
	$lock->releaseLock();
} catch (LockException $e) {
	// The lock could not be acquired... Let's ignore this.
}
```

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

4298d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1104771?v=4)[mouf](/maintainers/mouf)[@Mouf](https://github.com/Mouf)

---

Top Contributors

[![moufmouf](https://avatars.githubusercontent.com/u/1290952?v=4)](https://github.com/moufmouf "moufmouf (5 commits)")

---

Tags

mouflock

### Embed Badge

![Health badge](/badges/mouf-utilscommonlock/health.svg)

```
[![Health](https://phpackages.com/badges/mouf-utilscommonlock/health.svg)](https://phpackages.com/packages/mouf-utilscommonlock)
```

###  Alternatives

[pudongping/hyperf-wise-locksmith

A mutex library provider for the Hyperf framework, designed to enable serialized execution of PHP code in high-concurrency scenarios.

107.1k2](/packages/pudongping-hyperf-wise-locksmith)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
