- Sky
- Blueberry
- Slate
- Blackcurrant
- Watermelon
- Strawberry
- Orange
- Banana
- Apple
- Emerald
- Chocolate
- Charcoal
standinonstilts
-
Content Count
3 -
Joined
-
Last visited
Posts posted by standinonstilts
-
-
Did you ever find out? Because I'm trying to figure out the same thing
-
I want to create a server that pulls the modem_message event and prints to the console, but I also want the computer to be able to send messages (based on io.read()) while waiting for a message. I tried using threads, but I think I am using them wrong. As far as I know there is no thread.sleep() so I'm not sure how to suspend a thread for a short period of time so that you can still type messages, but still have that thread automatically continue. How would I go about doing this?
Multi Threaded Send and Receive
in Programming
Posted
my code was very close to yours. the only difference was that instead of creating a function inside the thread.create(), I created a seperate function named handleMessage() and called it as:
local t1 = thread.create(handleMessage())
whenever I do this, I cannot type input, it's like the thread is blocking the io.read; but when you define the function inside thread.create() it allows me to read and print perfectly fine. Is there a reason for this or is it a bug?
my code:
local event = require("event")
local thread = require("thread")
local modem = component.modem
modem.open(77)
function handleMessage()
while true do
local pack = table.pack(event.pull("modem_message"))
print(table.unpack(pack))
end
end
local t1 = thread.create(handleMessage())
while true do
local command = io.read()
print(command)
end
t1:kill()