Event loop

An event loop (english event loop, message dispatcher, message loop or message pump) is in the computer science a programming construct that waits for events or messages and distributes them within a program. Event sources are polled and the person responsible for the event or the message function is called.

Often, this loop represents the central control flow and is therefore referred to as main or main event loop.

Examples

Many modern programs have a main loop. Unlike before the loop is interrupted by pre-emptive multitasking and resumes only when there is something to actually process. This is more efficient than the active wait in cooperative multitasking.

Main loop of a program with cooperative multitasking

Function main      initialize ()      while program_running          message: = get_next_message ()          if message = no_message then              yield ()          else if message = quit then              return          end if          process_message (message)      repeat end function In this example provides get_next_message immediately " no_message " if no new messages are waiting. The function of yield () returns the remainder of the assigned processor time voluntarily ( cooperative), so that the system can still do something else, query as new messages.

Main loop of an application with pre-emptive multitasking

Function main      initialize ()      while program_running          message: = get_next_message ()          if message = quit then              return          end if          process_message (message)      repeat end function In this example, blocked get_next_message (without that the function knows about something ) until a new message comes. This is wasted with the queries of non-existent messages no time.

  • Programming
311406
de