Lua lock function for threads

Hello,

I was wondering if there is a way to lock a function in Lua as you can in C# to prevent multiple threads from accessing a function or event at the same time.

Thanks in advance :slight_smile:
Jop

Bump, anyone can help with this?

No one? :frowning:

can you say example !

1 Like

example !

3 Likes

I mean, you can make it a local function, but you wont be able to prevent it from being run inside the same .lua script if I understand it correctly. It will not be accessible by any other script, though.
Read this post on StackOverflow to gain a better idea.

1 Like

Thank you all for the replies @yeganehha , @nta , @_4iY .

What I am trying to do is the following:
GTA5 enforces an entity limit which prevents for instance 1000cars from being loaded/spawned into a client. However, I’d like to create vehicles all over the map that players can use or park somewhere else. To do this, I’ve created a streaming function.

All clients check wether a vehicle should be spawned in a radius around them. If this vehicle does not exist, they will create it and let the server dispose this vehicle once it is out of render distance.

However, the problem is, the check whether a vehicle should be spawned is done server side, where the clients spawn them. Now when 2 (or more) players enter the radius of a vehicle together, sometimes the vehicle gets spawned multiple times because the server gets called exactly at the sime time by 2 clients (threads). Inside the server function I’ve already created a boolean that should prevent double acces, however if the 2 threads call the function exactly at the same time this does not prevent the problem.

That isn’t directly related to a normal lock statement - you’re looking at distributed locking here which is pretty nasty, or maybe some sort of tie breaker (let the lowest numbered client in an area make the vehicle or so).

1 Like

Maybe some code to give an idea:

When a client intends te create a vehicle, it calls the following server event:
TriggerServerEvent("requestvehspawnlock", GetPlayerServerId(PlayerId()), vehindex)

The server than executes the following code:

RegisterNetEvent("requestvehspawnlock")
AddEventHandler("requestvehspawnlock",function(source, vehindex)
	if vehspawnlock[vehindex] == false then
		vehspawnlock[vehindex] = true
		TriggerClientEvent("returnvehspawnlock", source, true)
	else
		TriggerClientEvent("returnvehspawnlock", source, false)
	end
end)

The client spawns the car depending on the response, if false it will not spawn the car:

RegisterNetEvent('returnvehspawnlock')
AddEventHandler('returnvehspawnlock', function(response)
	AllowClientSpawnCar = response
	WaitingForServerLockResponse = false
end)

anyone, maybe @nta any ideas on the above code?

You should do a queue system with a system that save in memory the last car positions. You would be able to verifiy in player’s radius/coords how many cars there is and avoid double spawn. The problem of calling the function at the same time would be fixed by the queue system.