Avus
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 9768 |
|
|
| 02 Sep 2016 10:49 PM |
do u have any novel utility structures you like to use in ur code?
eg here's a quick and dirty C#-style thread lock
function lock(f) -- spawns new lock closure instance local locked = false local callF = function(...) while locked do wait() end locked = true f(...) locked = false end return callF end
it creates a section of locked code that only one thread can access at a time, here's a sample test case:
t = {} critical_code = lock(function(x) t[#t+1] = "A" wait(.1) t[#t+1] = "B" wait(.1) t[#t+1] = "C" wait(.1) t[#t+1] = x wait(.1) t[#t+1] = " " end)
function f() while wait(.05) do critical_code("f") end end
function g() while wait(.05) do critical_code("g") end end
spawn(f) spawn(g)
while wait(1) do print(table.concat(t)) end
normally, calling that function in the critical_code lock instance from those two threads so often would result in t[] being a jumbled up mess of A,B,C,f, and g -- since it takes .5 seconds to run that segment but f() and g() call it within of eachother
|
|
|
| Report Abuse |
|
|
Avus
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 9768 |
|
|
| 02 Sep 2016 10:50 PM |
| within a few moments of eachother** but instead, the output will be ABCf #### #### ABCg, showing that only one of them can access it at a time |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
| |
|
|
| 02 Sep 2016 11:25 PM |
| Does Kodran actually play ball hockey with the locals, if he does thats hilarious. |
|
|
| Report Abuse |
|
|
Avus
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 9768 |
|
|
| 02 Sep 2016 11:27 PM |
| more like bald hockey... kodrans head is shiny as h*ck |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 02 Sep 2016 11:29 PM |
i told u not to say swears im gonna have to report u :/
|
|
|
| Report Abuse |
|
|
Avus
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 9768 |
|
|
| 02 Sep 2016 11:31 PM |
| u mess with the bull u get the horns buddy |
|
|
| Report Abuse |
|
|
KreoBox
|
  |
| Joined: 24 Aug 2016 |
| Total Posts: 356 |
|
|
| 03 Sep 2016 12:30 AM |
OP is a scrub.
Learn how things work. |
|
|
| Report Abuse |
|
|
Avus
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 9768 |
|
| |
|