Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Ta©ti Tac0Z

does thread's interrupt

Question

my life started when i learned to use thread's and my life was completed when i learned the thread handles "detach" function but there is just one thing:

 

does threads interrupt or not, and i there do then i will like to learn how to provent that

 

help will be great --please

 

 

EDIT: interupt: when a program or a thread is geting a error runed by the keys: CTRL, ALT, C ( sry that i did't explain this better )

Edited by noahthegame
interupt = CTRL ALT C keys
Link to post
Share on other sites

11 answers to this question

Recommended Posts

  • 0
  • Solution
On 12/1/2017 at 9:26 AM, applejag said:

Example solution:


th = thread.create(function()
  while true do
    pcall(os.sleep, 1)

    computer.beep()
  end
end):detach()

 

@applejag Gave what I think is the best answer to your question. OpenOS functions that yield for and event can interrupt when the OS see's the ctrl+alt+c keys pressed. Basically most OS(kernel?) methods can throw an error in the user level(your program), if they receive the "interrupted" signal. If you run @applejag's example above, and pressed ctrl+alt+c to interrupt then it would be os.sleep that would get the interrupted event and throw and error into your program. Using the 'pcall' function will 'swallow' this error and thus preserve your program. One unintended side effect of this is that now you have to define your own method to exit this loop. Also with the example above when trying to interrupt the process you will probably cause a beep even if the timer hasn't elapsed yet.

Basically my advice would be to not do this in actual programs.. Learn and use 'soft' and 'hard' interrupts as defined with OpenOS. 'Hard' interrupts are in the OS for a good reason. To force a program to stop, like when you(or I) do something stupid, so we're not forced to restart the computer. 'Soft' interrupts will use the event system and can be pulled like any other event, allowing the program author to define program exit and cleanup on command from the user.

Link to post
Share on other sites
  • 1
41 minutes ago, noahthegame said:

--snip--

 

Upon an interrupt any event pulling function (for example os.sleep) throws an interrupt error, so one solution is to use pcall around all your event pulling function calls to silence those interrupt errors.

Example solution:

th = thread.create(function()
  while true do
    pcall(os.sleep, 1)

    computer.beep()
  end
end):detach()

 

Link to post
Share on other sites
  • 1

If you read the 'Interrupts' section I posted from the wiki it says that calls to computer.pullSignal, and thus event.pull* methods, will call error inside themselves to basically crash your program when ctrl alt c is held. Personally I don't know why you need an inescapable beeper not in foreground process but who am I to judge... I haven't tested this but I think you would simply call the raw coroutine.yield instead of computer or event methods, which really just wrap coroutine methods with some extra stuff.

th = thread.create(function()
  	while true do
      coroutine.yield(1) -- # This shouldn't recieve hard interrupts.. I could be wrong..
      computer.beep()
    end
  end)

th:detach()

EDIT:

If my above method works I'd stick with it. However @applejag  has a solution that should work nicely.

Link to post
Share on other sites
  • 0

Its really hard to understand your question. What I think might be happening is that the program you're testing the thread library in, or rather the parent process the thread is attached to, is receiving the interrupted event. When a threads 'host process' exits(ends) the attached processes 'join'(die). So to answer what I think your question is, stop pulling the interrupted event in whatever 'host process' is running your threads. FYI 'interrupted' events are queued by the OpenOS kernel and they bubble. See

Interrupts

Starting In OpenOS 1.6.4 and later, interrupts have been cleaned up. The following two methods are now obsolete

  • event.shouldSoftInterrupt(): boolean (Since 1.5.9 and removed in 1.6.4)
  • event.shouldInterrupt(): boolean (Since 1.5.9 and removed in 1.6.4)

Interrupts are a type of messaging intended to close or stop a process. In OpenOS the computer.pullSignal(), and thus any wrapper, generates 2 types of events.

They are especially useful when event.pull*() is called without time limit and with a filter. In some cases this means that event.pull*() could be waiting indefinitely.

  • Soft interrupts are an event signal generated by pressing Ctrl+C. The signal returns two fields, the event name "interrupted" and the computer uptime
  • Hard interrupts are generated by pressing Ctrl-Alt-C. It forcibly exits the event.pull*() method by throwing a "interrupted" error.

Basic event example

Typically user scripts care about one or two events, and don't care to handle the rest. It is good to handle “interrupted” for soft interrupts.

snippet.lua
while true do
  local id, _, x, y = event.pullMultiple("touch", "interrupted")
  if id == "interrupted" then
    print("soft interrupt, closing")
    break
  elseif id == "touch" then
    print("user clicked", x, y)
  end
end
Link to post
Share on other sites
  • 0

Ok, so when i ansert your questien for sone hours ago 

 

the time was like 6 in the morning 

 

So i forgot to write my exaple code so here it is:

Quote

 

 

And this is on my iPad so please understand that this is't easy:

 

th = thread.create( function() 

  While true do

    Os.sleep(1)

    Computer.beep()

  end

end ): detach()

 

 

that's it now remember to see the detach function :)

 

 

Now the thing is that is makeing the beep Sound and out side the program

 

but when i type ctrl alt c then it's stops so how to get it to ignore ctrl alt c

 

Link to post
Share on other sites
  • 0

Thanks guys for helping me it's just so nice

 

and yes the background beeping is inoring and was just and exaple

 

and yes i know that my englich is bad (but thank You two to not commend one it :))

 

so i know that i Can do like this forexaple:

 

function smartThread (fun, printError) --printError: for debug

  Return thread.create( function() 

    While true do

      s,r = xpcall(fun, debug.traceback) --make sure that a error don''t kill the thread

 

      If s == false and printError then

        Print("debug: error in thread: \n"..r) -- print a error if a error was detected and printError is true

      End

    End --if fails then just run it again

  end): detach()-- hmm detach or don't detach... i Don't know... does not matter

end

 

smartThread([place beeps here], true)

 

 

and again iPad i am srry 

 

but i don't like the thread to start from the begining evrey time i click ctrl alt c. So is this univiodeple?

 

and just one more questien:

 

so when i click on ctrl alt c the computer send a stop evrey thing You are doing event and the os.sleep function get that event and gets a error thats cool i never understood it before now thanks

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

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