I am trying to use locks between 2 Lua lanes,but observed that both the lanes are entering lock_func simultaneously..Below is the snippet
Code Snippet
==================
require"lanes"
local linda = lanes.linda()
lock_func = lanes.genlock(linda,"M",1)
local function lock_func()
print("Lock Acquired")
while(true) do
end
end
local function readerThread()
print("readerThread acquiring lock")
lock_func()
end
local function writerThread()
print("writerThread acquiring lock")
lock_func()
end
Thread1= lanes.gen("*",{globals = _G},writerThread)
Thread2= lanes.gen("*",{globals = _G},readerThread)
T1 = Thread1()
T2 = Thread2()
T1:join()
T2:join()
From the ouput below we can see both the lanes have entered the lock_func function simultaneously
output
==================
writerThread acquiring lock
Lock Acquired
readerThread acquiring lock
Lock Acquired
Is there any problem with the implementation of the lock from the above code?
The implementation of locks in lua can be done as in the below code snippet. Only the print from writer or reader lane will be executed from the below code, as which ever lane acquiring the lock will go into an infinite loop(just to see locks are working as expected) & other lane will wait for the lock to get released.
require"lanes"
local linda = lanes.linda()
f = lanes.genlock(linda,"M",1)
local function readerThread()
require"lanes"
f(1)
print("readerThread acquiring lock")
while(true) do
end
f(-1)
end
local function writerThread()
require"lanes"
f(1)
print("writerThread acquiring lock")
while(true) do
end
f(-1)
end
Thread1= lanes.gen("*",{globals = _G},writerThread)
Thread2= lanes.gen("*",{globals = _G},readerThread)
T1 = Thread1()
T2 = Thread2()
T1:join()
T2:join()