Forum

> > CS2D > Scripts > How make "Thread in lua"?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How make "Thread in lua"?

12 replies
To the start Previous 1 Next To the start

old How make "Thread in lua"?

shotnine
User Off Offline

Quote
I know how make "Thread" in Delphi but I don't know how make "Thread" in lua =(

what is it "Thread":
Example:
× BAD CODE:
1
2
3
function a()
	while true do end
end
error: stack overflow


√ GOOD CODE:
1
2
3
4
5
6
7
NewThread.Execute()
	while true do end
end

function a()
	NewThread.create()
end

old how it use???

shotnine
User Off Offline

Quote
IT'S not work:
1
2
3
4
5
6
7
co = coroutine.create(function ()
	while true do end
	print("hi")
end)

coroutine.resume(co)
print("WORK")

1
2
3
4
5
6
7
8
9
10
11
12
co = coroutine.create(function ()
           for i=1,10 do
             print("co", i)
             coroutine.yield()
           end
         end)

    coroutine.resume(co)    --> co   2
    coroutine.resume(co)    --> co   3
    ...
    coroutine.resume(co)    --> co   10
    coroutine.resume(co)    -- prints nothing
edited 1×, last 12.06.12 03:26:57 pm

old Re: How make "Thread in lua"?

Kisses
User Off Offline

Quote
user shotnine has written
IT'S not work:
1
2
3
4
5
6
7
co = coroutine.create(function ()
	while true do end
	print("hi")
end)

coroutine.resume(co)
print("WORK")


That's not the way they work as you have infinite loop in your script > while true do end

Please see http://www.lua.org/pil/9.1.html

note: Using coroutines only increases complexity and thus they are worthless. I suggest hooking "always" and doing your things there:
1
2
3
4
5
6
function threads()
	doSomething1()
	doSomething2()
	doSomething3()
end
addhook("always","threads")

old Re: How make "Thread in lua"?

shotnine
User Off Offline

Quote
I need "Thread" for use all cores.
CS2D use only 1 core

1
2
3
4
addhook("always","always")
function always()
	while true do end
end
LUA ERROR: (string 3) stack overflow

old Re: How make "Thread in lua"?

shotnine
User Off Offline

Quote
Kisses, Thank you!

but how use main vars
1
2
3
4
5
6
7
8
9
10
11
require "lanes"

f=lanes.gen(
function()
	return enyvar
end )

enyvar=1
a=f()

print(a)	---> nil
edited 1×, last 12.06.12 04:55:44 pm

old Re: How make "Thread in lua"?

Apache uwu
User Off Offline

Quote
@user shotnine: You can't nest variables within functions.

1
2
3
4
5
6
7
8
9
10
11
require "lanes"
 
f=lanes.gen(
	function(enyvar)
		return enyvar
	end)

enyvar=1
a=f(enyvar)
 
print(a)     ---> 1

old Re: How make "Thread in lua"?

shotnine
User Off Offline

Quote
user Apache uwu has written
@user shotnine: You can't nest variables within functions.

1
2
3
4
5
6
7
8
9
10
11
require "lanes"
 
f=lanes.gen(
	function(enyvar)
		return enyvar
	end)

enyvar=1
a=f(enyvar)
 
print(a)     ---> 1




1
2
3
4
5
6
7
require "lanes"
f=lanes.gen(
	function()
		image("gfx/cs2d.bmp",10,20,2)
	end)

f()
it's not work!

I FOUND!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require "lanes"

local a=1

local function SS()
	print(a)
end

local linda= lanes.linda()

local function funct()
	print("=)")
	SS()
	linda:send("x",a)
end

a=lanes.gen("",funct)()

b=linda:receive(3.0,"x")
print(b)
local for Thead = Global
edited 3×, last 13.06.12 05:41:07 pm

old Re: How make "Thread in lua"?

Kisses
User Off Offline

Quote
user Infinite Rain has written
Ofc it won't work!
Translated code:
if true is equals to true then repeat nothing

Infinity loop.

I hate it when people like you post worthless messages.

1st, I already told it's infinite loop.
2nd, your translation isn't correct.

old Re: How make "Thread in lua"?

Starkkz
Moderator Off Offline

Quote
Sorry for reviving this old thread but my curiosity has waken up. Has anyone tried making a coroutine in the main thread and then resume it in a Lua Lanes thread? I've done some similar tests from C code and I had success at trying to make two Lua states with different threads work with the same function environment. Maybe this attempt in Lua will result into a crash.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview