1

I wanted to share a global variable between 2 lanes,the idea is that when lane1 updates a shared variable,i should be able to get it's updated value on lane2 when it gets scheduled. Is there a solution to this?

Code Snippet below :-

shared_variable = 0
local function lane1()
    ..
    shared_variable = shared_variable + 1
end

local function lane2()
    ..
    print(shared_variable)-->shared variable is not getting updated,always prints 0
end

Thread1= lanes.gen("*",{globals = _G},lane1)
Thread2= lanes.gen("*",{globals = _G},lane2)

T1 = Thread1()
T2 = Thread2()

T1:join()
T2:join()
Anoop
  • 137
  • 1
  • 7

2 Answers2

2

Below is the sample implementation where we can share a varible between Lanes(using set & get method)

require("lanes")

shared_variable = 0

local linda = lanes.linda()

local function lane1()
    while true do
        shared_variable = shared_variable + 1
        linda:set("var", shared_variable)
    end
end

local function lane2()
    while true do
        local v = linda:get("var")
        print(v)
    end
end

Thread1= lanes.gen("*",{globals = _G},lane1)
Thread2= lanes.gen("*",{globals = _G},lane2)

T1 = Thread1()
T2 = Thread2()

T1:join()
T2:join()
Anoop
  • 137
  • 1
  • 7
1

You'll have to use lindas to synchronize the variable yourself - here's the documentation:

http://kotisivu.dnainternet.net/askok/bin/lanes/#lindas

And here's a fixed version of your code:

require("lanes")

shared_variable = 0

local linda = lanes.linda()

local function lane1()
    while true do
        shared_variable = shared_variable + 1
        linda:send("var", shared_variable)
    end
end

local function lane2()
    while true do
        local v = linda:receive("var")
        print(v)
    end
end

Thread1= lanes.gen("*",{globals = _G},lane1)
Thread2= lanes.gen("*",{globals = _G},lane2)

T1 = Thread1()
T2 = Thread2()

T1:join()
T2:join()
Gran PC
  • 21
  • 3
  • Thanks Gran..The solution you told is not i was looking for, this is going to create additional overhead & complexity into the implementation. As per Lua documentation there is a concept of luaG_deep_userdata() to resolve this kind of a issue,but i am missing on how to implement it from documentation.. – Anoop Oct 05 '13 at 10:52
  • @Anoop Deep userdata is a **Lanes** concept, not a Lua concept. It won't get simpler than using Linda, though you would want to use `get` and `set` instead of `send`/`receive` (search for `:get` in Lanes doc) – dualed Oct 06 '13 at 17:27
  • @dualed I was actually using `get` and `set` at first when answering this question, but it caused Lua to crash... – Gran PC Oct 07 '13 at 09:35
  • 1
    @GranPC thats unfortunate, but implementing a "SHM" with send/receive is a bit convoluted, you'd have to do something like `local lock = lanes.genlock(...); [...] lock(1); local x = linda:receive(foo); linda:send(foo, x); lock(-1); ` and thats just for `set`. meh. :) I think it's better to resolve the issue of the crash. – dualed Oct 07 '13 at 13:03
  • Sending & receiving messages for updating a shared variable is not a good concept. The reason is, i am planning to schedule one of the threads(T1) 5sec with timer & other thread(T2) 0.5 sec. To send message from T2 to T1 beats the purpose of T1 going to deep sleep. That's where i was looking for a mechanism of shared variable where T1 can use the shared variable which T2 might have updated. – Anoop Oct 07 '13 at 18:41
  • @Anoop I hope you don't have GranPC's issues, if you do, the code I gave in the comment above should work, with some fixes (receive needs a 0 argument before foo I think) as it is dry code. The idea is that whenever you want to read from a variable, you "receive" it from the queue (variable) then write it back right away so that when you look at the queue again, the value is there again. Since that needs to be atomic, you have to surround it with a lock. For set, you would do the same, just throw away the value you "receive"d. Again this should fit your use case, but it has needless overhead – dualed Oct 07 '13 at 23:35
  • @dualed/GranPC i dont haven't observed the crash issue..Will post my sample code as answer to this thread – Anoop Oct 08 '13 at 04:07