Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal

standinonstilts

Members
  • Content Count

    3
  • Joined

  • Last visited

Reputation Activity

  1. Upvote
    standinonstilts reacted to payonel in Multi Threaded Send and Receive   
    > I want to create a server that pulls the modem_message event and prints to the console
    Cool, you can `listen` or `pull` for that event
    > but I also want the computer to be able to send messages (based on io.read()) while waiting for a message.
    `listen` works well for background handling, while in the foreground you are doing other work (such as io.read). However, you want your background to also print to the console? That'll obscure/interfere (visually) with the io.read getting user input. But okay
    > I tried using threads
    Sure, threads can do this job too. With threads, your event handling can be designed in a more selfish manner. You can `event.pull` and such without worrying about blocking the rest of the system
    > but I think I am using them wrong.
    Feel free to share some code!
    > 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
    No, there isn't. But, calling `os.sleep` from inside the thread is identical to what you would expect from `thread.sleep`. **HOWEVER** (please keep reading)
    > so that you can still type messages, but still have that thread automatically continue. How would I go about doing this?
    I wouldn't call `os.sleep` in THIS program because you want your thread to handle specific messages. `os.sleep` is a RUDE sleep, it DROPS events in the garbage. If any modem_messages occur during the sleep, you missed them. Instead, just pull with a timeout, `event.pull(.5, "modem_message")` in fact, I don't see the point of giving a timeout at all. Is your modem_message thread doing anything else or just waiting for modem_messages? Just call `event.pull("modem_message")` in your thread, done
    local thread = require("thread") local event = require("event") local t = thread.create(function() while true do local pack = table.pack(event.pull("modem_message")) print(table.unpack(pack)) end end) while true do local command = io.read() if command == "quit" then break end end t:kill()  
    BTW: os.sleep *does not* rob events from any other thread or process, it only throws away events of its own process
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.