LuaTask and other thread libraries

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

LuaTask and other thread libraries

Francisco Sant'anna-2
Hello,

When the use of threads is required, what's the best option I have?
I found:
1 - www.cs.princeton.edu/~diego/professional/luathread/
2 - lua51pthread.luaforge.net/
3 - luaforge.net/projects/luatask/
I chose LuaTask because of its simplicity and straightforward use.

Also, I couldn't find an active LuaTask forum or list, sorry for posting here.

When a thread raises an error, how am I supposed to notice it?
I can use task.isrunning() but where's the error message?
In the simple example below I can never see the message "hello error".

-- t1.lua
require 'task'
task.create't2.lua'

-- t2.lua
error'hello error'

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: LuaTask and other thread libraries

Daniel Quintela
Francisco Sant'anna escribió:
Hello,

When the use of threads is required, what's the best option I have?
I found:
1 - www.cs.princeton.edu/~diego/professional/ <http://www.cs.princeton.edu/%7Ediego/professional/>*luathread*/
*2 - lua*51*pthread*.*lua*forge.net/ <http://forge.net/>
3 - luaforge.net/projects/ <http://luaforge.net/projects/>*luatask*/
I chose LuaTask because of its simplicity and straightforward use.

Also, I couldn't find an active LuaTask forum or list, sorry for posting here.

When a thread raises an error, how am I supposed to notice it?
I can use task.isrunning() but where's the error message?
In the simple example below I can never see the message "hello error".

-- t1.lua
require 'task'
task.create't2.lua'

-- t2.lua
error'hello error'

Thanks.

Hi Francisco,

There is a forum at http://luaforge.net/forum/forum.php?forum_id=203

About your example:
1) t1.lua (the main task) must do something after task.create() to avoid terminating the whole program.
   -- t1.lua
   require 'task'
   task.create't2.lua'
   task.receive( -1 ) -- main task blocked forever
2) The "error" function push the error message on the stack and terminates the pcall. LuaTask doesn't invoke a report function (message printing) as lua binary does, and you never see (or get) the message. You can replace "error" by "print" in t2.lua to successfully run your example.

Regards,
Daniel



Reply | Threaded
Open this post in threaded view
|

Re: LuaTask and other thread libraries

Daniel Quintela
Daniel Quintela escribió:
Francisco Sant'anna escribió:
Hello,

When the use of threads is required, what's the best option I have?
I found:
1 - www.cs.princeton.edu/~diego/professional/ <http://www.cs.princeton.edu/%7Ediego/professional/>*luathread*/
*2 - lua*51*pthread*.*lua*forge.net/ <http://forge.net/>
3 - luaforge.net/projects/ <http://luaforge.net/projects/>*luatask*/
I chose LuaTask because of its simplicity and straightforward use.

Also, I couldn't find an active LuaTask forum or list, sorry for posting here.

When a thread raises an error, how am I supposed to notice it?
I can use task.isrunning() but where's the error message?
In the simple example below I can never see the message "hello error".

-- t1.lua
require 'task'
task.create't2.lua'

-- t2.lua
error'hello error'

Thanks.

Hi Francisco,

There is a forum at http://luaforge.net/forum/forum.php?forum_id=203

About your example:
1) t1.lua (the main task) must do something after task.create() to avoid terminating the whole program.
   -- t1.lua
   require 'task'
   task.create't2.lua'
   task.receive( -1 ) -- main task blocked forever
2) The "error" function push the error message on the stack and terminates the pcall. LuaTask doesn't invoke a report function (message printing) as lua binary does, and you never see (or get) the message. You can replace "error" by "print" in t2.lua to successfully run your example.

Regards,
Daniel


Here is a simple rewrite of the example:


-- t1.lua
require 'task'
task.create( 't2.lua')
local msg, flags = task.receive( -1 )
if flags then print( msg ) end

-- t2.lua
local ok, err = pcall( function()

print( 'printing inside t2' )
error( 'hello error' )

end )
if ok then task.post( 1, '', 0 ) else task.post( 1, err or '?', -1 ) end


Regards,
Daniel



Reply | Threaded
Open this post in threaded view
|

Re: LuaTask and other thread libraries

Francisco Sant'anna-2

Here is a simple rewrite of the example:


-- t1.lua
require 'task'
task.create( 't2.lua')
local msg, flags = task.receive( -1 )
if flags then print( msg ) end

-- t2.lua
local ok, err = pcall( function()

print( 'printing inside t2' )
error( 'hello error' )

end )
if ok then task.post( 1, '', 0 ) else task.post( 1, err or '?', -1 ) end

This pcall solution is much cleaner, very tricky.
I'm using LuaTask with wxLua and the result is great.
The GUI starts a LuaTask and in idle events calls task.receive(0) to update it's LOG text control.
As you suggested I was rewriting error() and assert(), but this method is better.
With the flag parameter I can use 1=control, 2=log, 3=error.
Very simple, very effective, thank you for this great module.
I still miss a way to cancel a task, but I'm aware of the portability/no support issues.

Regards,
Francisco