青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 297,  comments - 15,  trackbacks - 0

Callback (computer science)

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.

A callback is often back on the level of the original caller.

However, while technically accurate, this might not be the most illustrative explanation. Think of it as an "In case of fire, break glass" subroutine. Many computer programs tend to be written such that they expect a certain set of possibilities at any given moment. If "Thing That Was Expected", then "Do something", otherwise, "Do something else." is a common theme. However, there are many situations in which events (such as fire) could happen at any time. Rather than checking for them at each possible step ("Thing that was expected OR Things are on fire"), it is easier to have a system which detects a number of events, and will call the appropriate function upon said event (this also keeps us from having to write programs like "Thing that was expected OR Things are on fire OR Nuclear meltdown OR alien invasion OR the dead rising from the grave OR...etc., etc.) Instead, a callback routine is a sort of insurance policy. If zombies attack, call this function. If the user moves their mouse over an icon, call HighlightIcon, and so forth.

Usually, there is a framework in which a series of events (some condition is met) in which the running framework (be it a generic library or unique to the program) will call a registered chunk of code based on some pre-registered function (typically, a handle or a function pointer) The events may be anything from user input (such as mouse or keyboard input), network activity (callbacks are frequently used as message handlers for new network sessions) or an internal operating system event (such as a POSIX-style signal) The concept is to develop a piece of code that can be registered within some framework (be it a GUI toolkit, network library, etc.) that will serve as the handler upon the condition stated at registration. How the flow of control is passed between the underlying framework and the registered callback function is specific to the framework itself.

In another common scenario, the callback is first registered and later called asynchronously.

Contents

[hide]

[edit] Motivation

To understand the motivation for using callbacks, consider the problem of a network server. At any given point in time, it may have an internal state machine that is currently at a point in which it is dealing with one very specific communication session, not necessarily expecting new participants. As a host, it could be dealing with all the name exchange and handshakes and pleasantries, but no real way of dealing with the next dinner party guest that walks through the door. One way to deal with this is for this server to live by a state machine in which it rejects new connections until the current one is dealt with...not very robust (What if the other end goes away unexpectedly?) and not very scalable (Would you really want to make other clients wait (or more likely, keep retrying to connect) until it's their turn?) Instead, it's easier to have some sort of management process that spins off a new thread (or process) to deal with the new connection. Rather than writing programs that keep dealing with all of the possible resource contention problems that could come of this, or all of the details involved in socket code (your desired platform may be more straight-forward than others, but one of your design goals may be cross-platform compatibility), many have opted to use more generic frameworks that will handle such details in exchange for providing a reference such that the underlying framework can call it if the registered event occurs.

[edit] Example

The following code in C demonstrates the use of callbacks for the specific case of dealing with a POSIX-style signal (in this case SIGUSR1).

#include <stdio.h>
#include <signal.h>
void * sig(int signum)
{
printf("Received signal number %d!\n", signum);
}
int main(int argc, char *argv[])
{
signal(SIGUSR1,&sig);
while(1){};
return 0;
}

The while loop will keep this example from doing anything interesting, but it will give you plenty of time to send a signal to this process. (If you're on a unix-like system, try a "kill -USR1 <pid>" to the process ID associated with this sample program. No matter how or when you send it, the callback should respond.)

[edit] Implementation

The form of a callback varies among programming languages.

  • C and C++ allow function pointers as arguments to other functions.
  • Several programming languages (though especially functional programming languages such as Scheme or ML) allow closures, a generalization of function pointers, as arguments to other functions.
  • Several programming languages, especially interpreted languages, allow one to pass the name of a function A as a parameter to a function B and have B call A by means of eval.
  • In object-oriented programming languages, a call can accept an object that implements some abstract interface, without specifying in detail how the object should do so. The programmer who implements that object may use the interface's methods exclusively for application-specific code. Such objects are effectively a bundle of callbacks, plus the data they need to manipulate. They are useful in implementing various design patterns like Visitor, Observer, and Strategy.
  • C++ allows objects to provide their own implementation of the function call operation. The Standard Template Library accepts these objects (called functors), as well as function pointers, as parameters to various polymorphic algorithms
  • C# .NET Framework provides a type-safe encapsulating reference, a 'delegate', to manage function pointers. These can be used for callback operations.
  • Perl supports subroutine references.[1][2]
  • Some systems have built-in programming languages to support extension and adaptation. These languages provide callbacks without the need for separate software development tools.

[edit] Special cases

Callback functions are also frequently used as a means to handle exceptions arising within the low level function, as a way to enable side-effects in response to some condition, or as a way to gather operational statistics in the course of a larger computation. Interrupt handlers in an operating system respond to hardware conditions, signal handlers of a process are triggered by the operating system, and event handlers process the asynchronous input a program receives.

A pure callback function is one which is purely functional (always returns the same value given the same inputs) and free of observable side-effects. Some uses of callbacks require pure callback functions to operate correctly.

A special case of a callback is called a predicate callback, or just predicate for short. This is a pure callback function which accepts a single input value and returns a Boolean value. These types of callbacks are useful for filtering collections of values by some condition.

[edit] See also

[edit] External links

[edit] References

posted on 2009-05-12 23:28 chatler 閱讀(561) 評論(0)  編輯 收藏 引用 所屬分類: OS
<2010年8月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

常用鏈接

留言簿(10)

隨筆分類(307)

隨筆檔案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品日韩一区二区| 久久综合色播五月| 国产色综合久久| 国产欧美日韩免费| 国产欧美日韩三级| 亚洲国产电影| 亚洲一区二区三区四区视频| 国产精品99久久久久久久vr| 欧美在线一二三四区| 欧美久久久久免费| 欧美成在线观看| 午夜在线一区| 欧美mv日韩mv亚洲| 亚洲国产精品毛片| 亚洲精品在线视频| 欧美在线一区二区三区| 欧美高清在线观看| 有码中文亚洲精品| 蜜臀va亚洲va欧美va天堂| 久久只精品国产| 欧美久久九九| 亚洲国内高清视频| 久久久久9999亚洲精品| 亚洲精品裸体| 老巨人导航500精品| 国产精一区二区三区| 亚洲三级免费| 欧美gay视频| 久久精品亚洲| 伊人久久大香线| 欧美成人精品一区二区三区| 欧美一区高清| 最新国产乱人伦偷精品免费网站 | 欧美视频二区| 99精品99| 亚洲自拍偷拍麻豆| 国产美女精品| 乱人伦精品视频在线观看| 久久黄色影院| 日韩视频免费看| 亚洲欧美日韩系列| 国产一区二区三区久久 | 久久日韩粉嫩一区二区三区| 午夜激情久久久| 在线观看日韩av先锋影音电影院 | 亚洲国产aⅴ天堂久久| 欧美激情视频一区二区三区在线播放| 久久精品综合网| 新67194成人永久网站| 久久综合五月天婷婷伊人| 久久深夜福利免费观看| 久久中文精品| 亚洲在线视频一区| 欧美一区二区精美| 亚洲三级色网| 久久久久国产一区二区| 一区二区欧美在线观看| 久久激五月天综合精品| 夜夜夜久久久| 久久免费国产精品| 理论片一区二区在线| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 欧美日韩在线高清| 麻豆成人在线播放| 国产精品美女在线观看| 欧美激情一区二区在线 | 女人天堂亚洲aⅴ在线观看| 欧美亚洲在线观看| 亚洲男女毛片无遮挡| 亚久久调教视频| 999亚洲国产精| 欧美激情在线狂野欧美精品| 久久久国产精品一区| 国产亚洲精品久久久久久| 亚洲专区在线视频| 久久久久久精| 在线免费观看一区二区三区| 99re热这里只有精品视频| 欧美成人有码| 亚洲一区二区三区四区在线观看 | 亚洲电影中文字幕| 国内精品久久久久久久影视麻豆| 亚洲视频一二区| 久久久噜噜噜| 99国产精品99久久久久久| 国产精品久久久99| 久久狠狠婷婷| 新片速递亚洲合集欧美合集| 鲁大师成人一区二区三区| 欧美人妖另类| 亚洲一区二区三区免费视频| 久久久www成人免费精品| 在线亚洲欧美专区二区| 亚洲精品美女久久7777777| aa级大片欧美| 久久爱www久久做| 欧美一区视频在线| 一本一本a久久| 日韩小视频在线观看专区| 欧美成人中文字幕| 久久久久免费视频| 午夜视黄欧洲亚洲| 亚洲视频一区| 亚洲精品久久久蜜桃| 伊人精品成人久久综合软件| 欧美性理论片在线观看片免费| 久久午夜精品| 久久一区视频| 久久尤物电影视频在线观看| 久久久久久9999| 久久亚洲一区二区三区四区| 欧美呦呦网站| 老色批av在线精品| 乱码第一页成人| 欧美性色综合| 国产在线欧美| 亚洲破处大片| 欧美成人免费全部| 亚洲免费久久| 亚洲欧美中日韩| 麻豆国产精品va在线观看不卡| 久久精品国内一区二区三区| 久久久久网站| 欧美日韩免费一区| 国产亚洲成av人在线观看导航 | 久久成人免费日本黄色| 久久精品国产亚洲一区二区三区| 老司机午夜精品视频在线观看| 毛片一区二区| 亚洲一区二区视频在线观看| 久久九九精品| 国产精品成av人在线视午夜片| 黄色成人av| 亚洲午夜小视频| 亚洲国产经典视频| 久久精品99国产精品日本| 欧美视频在线一区| 亚洲综合电影一区二区三区| 99re在线精品| 欧美性做爰毛片| 欧美一区二区精品| 欧美一区二区三区啪啪| 黄网动漫久久久| 亚洲精品国产精品国产自| 欧美三区在线| 免费观看国产成人| 99pao成人国产永久免费视频| 校园春色国产精品| 久久中文字幕一区| 久久精品亚洲一区| 国产精品一国产精品k频道56| 一区二区三区久久久| 亚洲人体偷拍| 欧美午夜激情小视频| 亚洲视频成人| 亚洲一区二区视频| 久久中文字幕一区| 另类欧美日韩国产在线| 在线观看日韩专区| 亚洲黄一区二区三区| 欧美激情久久久久久| 一区二区高清| 亚洲欧美日韩精品一区二区| 狠狠爱www人成狠狠爱综合网| 久久久青草青青国产亚洲免观| 久久久九九九九| 在线视频欧美日韩| 欧美h视频在线| 在线日韩一区二区| 欧美大尺度在线| 国产精品视频免费观看| 久久精品日产第一区二区| 欧美激情一区二区三区高清视频| 亚洲一区二区四区| 美女脱光内衣内裤视频久久影院| 亚洲伦伦在线| 美日韩丰满少妇在线观看| 亚洲男人天堂2024| 欧美成人资源网| 欧美电影免费观看高清完整版| 国产精品毛片| 日韩一级欧洲| 一区二区三区四区五区精品视频| 欧美中文字幕| 欧美在线视频一区二区三区| 欧美日韩mp4| 亚洲毛片在线观看| 日韩一区二区精品| 欧美电影在线免费观看网站| 久久综合成人精品亚洲另类欧美| 国产精品免费一区二区三区在线观看 | 久久成人一区| 久久精品国产亚洲一区二区| 国产欧美日韩综合| 亚洲伊人观看| 久久视频一区二区| 欧美亚洲日本一区| 久久亚洲欧美| 亚洲精品综合久久中文字幕| 欧美精品在线观看91|