generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: ITT: fun and/or useful design patterns

Previous Thread :: Next Thread 
Avus is not online. 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 is not online. 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 is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
02 Sep 2016 11:24 PM
don't swear!


Report Abuse
bosswalrus is not online. bosswalrus
Joined: 04 Jan 2013
Total Posts: 5430
02 Sep 2016 11:25 PM
Does Kodran actually play ball hockey with the locals, if he does thats hilarious.
Report Abuse
Avus is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. Avus
Joined: 05 Aug 2009
Total Posts: 9768
03 Sep 2016 09:01 AM
what do u mean
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image