# async.mutex (Compartilhado)

> Roda uma função numa thread async nova, como `async.thread`, mas não começa enquanto outra thread iniciada pela mesma linha do código, com a mesma `key`, ainda está rodando.

- **Plataforma**: MY Multiplayer (MYMP)
- **Tipo**: Função Lua
- **Lado**: Compartilhado
- **Categoria**: Tempo e assíncrono
- **URL**: https://wiki.mymp.gg/pt/async.mutex

## Sintaxe

```lua
any async.mutex(function func [, any key = nil])
```

## Parâmetros

| Nome | Tipo | Descrição |
| --- | --- | --- |
| `func` | `function` | Função que roda na thread. |
| `key` | `any` | Valor que separa as travas da mesma linha, como um jogador. Ele é convertido em texto, como faz `tostring`. *(padrão: `nil`)* |

## Retorno

A thread, ou `false` se uma thread da mesma linha e da mesma `key` ainda não terminou.

## Exemplo

```lua
addCommandHandler("save", function(player)
    local started = async.mutex(function()
        async.wait(2000)
        outputDebugString(getPlayerName(player) .. " salvou")
    end, player)

    if not started then
        outputDebugString(getPlayerName(player) .. " já está salvando")
    end
end)
```

## Veja também

- [async.thread (Compartilhado)](https://wiki.mymp.gg/pt/async.thread.md): Roda uma função numa thread async nova, na hora. A chamada retorna quando a função termina ou faz a primeira pausa, como async.wait.
- [async.status (Compartilhado)](https://wiki.mymp.gg/pt/async.status.md): Diz em que estado está uma thread async deste recurso.
