This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Aragon0's profile picture

[1.4] Multithreading & Redirecting Output

Started by Aragon0, 25 April 2012 - 09:51 AM
Aragon0 #1
Posted 25 April 2012 - 11:51 AM
I think that multithreading would be very useful, I have also an idea how to do it:


function test()
end
os.callAsync("test")

What do you think about?

Redirecting Output would also help to make anwesome programs like remonte controlling and so. My idea was:

output.beginRedirect()
print("Something...")
Output=output.endRedirect()

I hope it will be implemented.

PS: Sorry for my english…
Xfel #2
Posted 25 April 2012 - 12:35 PM
Real multithreding isn't supported by lua. However, one could extend the parallel api to allow dynamic addition of coroutines. Some kind of fork function is definetely necessairy
For the other thing: You can redirect the output by using term.redirect. print and write only call the term functions. A friend of mine wrote some kind of ssh program using these.
Leo Verto #3
Posted 25 April 2012 - 01:16 PM
For the other thing: You can redirect the output by using term.redirect. print and write only call the term functions. A friend of mine wrote some kind of ssh program using these.
SSH? A working security system for CC messages? Interesting, I would like to know more about that.
Cloudy #4
Posted 25 April 2012 - 01:19 PM
The parallel API is how you should do multi threading - the API uses coroutines. The bios in fact calls a coroutine for rednet at the same time as the shell.

I don't think a fork function is necessary - parallel works perfectly fine.
OminousPenguin #5
Posted 25 April 2012 - 01:27 PM
Seeing as Java supports multithreading, would it not be possible to expose that through CC?
Xfel #6
Posted 25 April 2012 - 02:53 PM
The parallel API is how you should do multi threading - the API uses coroutines. The bios in fact calls a coroutine for rednet at the same time as the shell.

I don't think a fork function is necessary - parallel works perfectly fine.
If you want some kind of background program, you need it. And that can't be done with parallel.

Seeing as Java supports multithreading, would it not be possible to expose that through CC?
No. The architecture of lua isn't meant to handle syncronisation & co, so there would be a big potential source for errors. Coroutine is good enough.
OminousPenguin #7
Posted 25 April 2012 - 04:54 PM
The parallel API is how you should do multi threading - the API uses coroutines. The bios in fact calls a coroutine for rednet at the same time as the shell.

I don't think a fork function is necessary - parallel works perfectly fine.
If you want some kind of background program, you need it. And that can't be done with parallel.

Seeing as Java supports multithreading, would it not be possible to expose that through CC?
No. The architecture of lua isn't meant to handle syncronisation & co, so there would be a big potential source for errors. Coroutine is good enough.

First you say "you need it. And that can't be done with parallel." and then you say "Coroutine is good enough."… Maybe I'm misunderstanding you but it seems you contradict yourself.

Anyway. The Lua is compiled into Java bytecode and Java does support threads and synchronisation. The fact that Lua doesn't is irrelevant. I don't know how good a programmer the author of CC is and I'm not going to make any assumptions about whether he'd be able to implement it error free. The fact is that it would be possible.
Leo Verto #8
Posted 25 April 2012 - 05:05 PM
Why don't you just use several computers? :)/>/>
Aragon0 #9
Posted 25 April 2012 - 08:39 PM
If Multithreading isn't possible, is it possible to run many programs at the same time, like in Linux with the &? So you can run a server in the background by typing "server.lua &"? That would help a lot. And you can run the second program in an external java thread, that should be possible.

What do you think about that?
Cloudy #10
Posted 25 April 2012 - 10:15 PM
The parallel API is how you should do multi threading - the API uses coroutines. The bios in fact calls a coroutine for rednet at the same time as the shell.

I don't think a fork function is necessary - parallel works perfectly fine.
If you want some kind of background program, you need it. And that can't be done with parallel.

Really?
while true do
	parallel.waitForAny(yourBackgroundFunc(), function() shell.run("shell") end)
end

There, backgrounded process which runs while shell does. Not to mention you can code this in Lua without a built in function - I know a person who has coded his own OS replacement complete with background processes, all without editing bios.lua.

Anyway. The Lua is compiled into Java bytecode and Java does support threads and synchronisation. The fact that Lua doesn't is irrelevant. I don't know how good a programmer the author of CC is and I'm not going to make any assumptions about whether he'd be able to implement it error free. The fact is that it would be possible.

It wouldn't be worth the effort - Lua is not designed to be multi threaded apart from coroutines and he would have to substantially change it.