Forum

> > CS2D > Scripts > How make "Thread in lua"?
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch How make "Thread in lua"?

12 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt How make "Thread in lua"?

shotnine
User Off Offline

Zitieren
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

alt how it use???

shotnine
User Off Offline

Zitieren
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
1× editiert, zuletzt 12.06.12 15:26:57

alt Re: How make "Thread in lua"?

Kisses
User Off Offline

Zitieren
user shotnine hat geschrieben
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")

alt Re: How make "Thread in lua"?

shotnine
User Off Offline

Zitieren
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

alt Re: How make "Thread in lua"?

shotnine
User Off Offline

Zitieren
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
1× editiert, zuletzt 12.06.12 16:55:44

alt Re: How make "Thread in lua"?

Apache uwu
User Off Offline

Zitieren
@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

alt Re: How make "Thread in lua"?

shotnine
User Off Offline

Zitieren
user Apache uwu hat geschrieben
@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
3× editiert, zuletzt 13.06.12 17:41:07

alt Re: How make "Thread in lua"?

Kisses
User Off Offline

Zitieren
user Infinite Rain hat geschrieben
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.

alt Re: How make "Thread in lua"?

Starkkz
Moderator Off Offline

Zitieren
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.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht