swoole_atomic is an atomic count operation class provided by the swoole extension, which can facilitate the increase and decrease of integer-free lock-free atoms.

  • swoole_atomic uses shared memory to manipulate counts between different processes
  • swoole_atomic is based on the CPU atomic instructions provided by gcc, no need to lock
  • swoole_atomic must be created before the swoole_server->start in the server program to be used in the Worker process.
  • swoole_atomic uses 32-bit unsigned types by default. If you need 64-signed integers, you can use Swoole\Atomic\Long.

Note: Do not create atomic numbers in callback functions such as onReceive, otherwise the underlying GlobalMemory memory will continue to grow, causing memory leaks.


Initialization count:

  • Int init_value initial value
public function __construct($init_value)

Increase the count:

  • Int add_value increased value
public function add($add_value)

Reduce the count:

  • Int sub_value reduced value
public function sub($sub_value)

Get the current count value:

public function get()

Set the current value to the specified number:

  • int value count value
public function set($value)

If the current value is equal to parameter 1, the current value is set to parameter 2:

  • int cmp_value is the value to be compared
  • int set_value is set to the specified count value after the current value is equal to the value being compared
public function cmpset($cmp_value, $set_value)


Register the atomic object in the `EasySwooleEvent initialization function.

// Register an atomic object

Get the atomic object from AtomicManager and use it.

 * Created by PhpStorm.
 * User: Tioncico
 * Date: 2019/3/18 0018
 * Time: 15:39

namespace App\HttpController;

use EasySwoole\Component\AtomicManager;
use EasySwoole\Http\AbstractInterface\Controller;

class Index extends Controller
    function index()

        $atomic = AtomicManager::getInstance()->get('second');
        // TODO: Implement index() method.