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

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 閱讀(567) 評論(0)  編輯 收藏 引用 所屬分類: OS
<2009年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

常用鏈接

留言簿(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>
            国产拍揄自揄精品视频麻豆| 韩国女主播一区| 亚洲精品影视| 亚洲激情av| 欧美激情aaaa| 一区二区三区导航| 99天天综合性| 国产免费成人av| 久久精品中文| 久久只有精品| 正在播放日韩| 亚洲欧美第一页| 国产综合一区二区| 欧美国产精品| 国产精品sss| 久久夜色精品国产欧美乱极品| 欧美一区2区视频在线观看 | 亚洲国产婷婷香蕉久久久久久| 裸体一区二区| 亚洲中字在线| 久久成人18免费网站| 亚洲高清在线| 亚洲性图久久| 在线成人h网| 亚洲乱码国产乱码精品精可以看| 国产精品久久久久一区二区| 玖玖玖免费嫩草在线影院一区| 欧美激情精品久久久久久久变态| 亚洲一区二区av电影| 久久国产精品色婷婷| 亚洲精品在线观看免费| 亚洲在线观看视频| 亚洲黑丝在线| 欧美亚洲一区二区在线观看| 亚洲国产视频a| 亚洲女同性videos| 亚洲精品资源美女情侣酒店| 亚洲一级片在线观看| 91久久精品国产| 欧美亚洲在线播放| 亚洲一区二区三区涩| 男女精品视频| 久久全国免费视频| 国产精品久久久久久户外露出| 欧美黄色免费| 国产视频亚洲精品| 亚洲人成网站在线观看播放| 国模精品一区二区三区| 一区二区欧美日韩| 99国产精品国产精品久久| 久久精品国产清自在天天线| 亚洲欧美久久久| 欧美日韩一区综合| 最新高清无码专区| 亚洲国产精品电影| 欧美一区亚洲一区| 欧美亚洲日本一区| 国产精品久久久久免费a∨| 欧美电影电视剧在线观看| 国产真实乱子伦精品视频| 亚洲一区在线观看视频| 中日韩在线视频| 欧美日韩无遮挡| 亚洲免费久久| 日韩一区二区精品葵司在线| 美女福利精品视频| 欧美成人国产va精品日本一级| 国产香蕉久久精品综合网| 亚洲女人av| 久久不射中文字幕| 国产乱码精品一区二区三区五月婷 | 国语自产偷拍精品视频偷 | 免费日韩成人| 精品99视频| 久久综合一区二区| 欧美激情一区二区三区不卡| 最近看过的日韩成人| 欧美jizzhd精品欧美喷水| 欧美国产一区二区在线观看| 亚洲伦理自拍| 欧美色一级片| 亚洲欧美另类在线观看| 久久蜜桃av一区精品变态类天堂| 国内精品一区二区| 麻豆精品精华液| 亚洲精品视频啊美女在线直播| 一区二区三区蜜桃网| 国产精品二区三区四区| 亚洲欧美另类在线| 欧美成人按摩| 一本色道久久88综合亚洲精品ⅰ| 国产精品v一区二区三区| 小黄鸭精品密入口导航| 免播放器亚洲| 夜夜嗨av一区二区三区四区| 国产精品久久久久久久久久免费看| 亚洲欧美日韩一区在线观看| 欧美91精品| 亚洲一区精品视频| 黑人操亚洲美女惩罚| 欧美极品在线观看| 欧美亚洲一区二区在线观看| 亚洲第一伊人| 欧美一区二区日韩一区二区| 亚洲国产精品一区| 国产精品国产三级国产普通话蜜臀 | 老色鬼久久亚洲一区二区| 亚洲国产一区二区视频| 欧美日韩国产一区精品一区 | 亚洲高清视频一区| 亚洲综合欧美日韩| 亚洲大片免费看| 欧美日韩一区二区三区在线观看免| 午夜精品婷婷| 99re视频这里只有精品| 男女激情视频一区| 新狼窝色av性久久久久久| 亚洲欧洲一区二区三区| 国产日韩欧美a| 欧美日韩高清免费| 久久亚洲影音av资源网| 亚洲永久在线| 99在线精品观看| 欧美激情视频在线播放| 欧美在线观看你懂的| 99精品国产一区二区青青牛奶| 黑人操亚洲美女惩罚| 国产精品亚洲а∨天堂免在线| 欧美电影打屁股sp| 久久免费国产精品| 欧美一区二区三区视频| 一本色道久久88亚洲综合88| 欧美黄色视屏| 欧美黄色精品| 欧美高清视频一二三区| 久久只有精品| 久久在线免费视频| 久久深夜福利| 久久久久国产精品午夜一区| 午夜精品一区二区三区在线播放| 一区二区三区www| 99国产精品自拍| 亚洲看片一区| 日韩亚洲视频| 亚洲美女中文字幕| 日韩系列在线| 99亚洲伊人久久精品影院红桃| 亚洲精品在线视频| 99爱精品视频| 亚洲视频成人| 亚洲影院在线| 香蕉成人久久| 久久精品中文字幕一区二区三区| 欧美一区二区三区精品电影| 欧美在线精品一区| 久久久综合激的五月天| 久久综合给合久久狠狠色 | 亚洲午夜精品视频| 亚洲视频一区| 午夜一区在线| 久久成人免费视频| 久久夜色精品国产噜噜av| 欧美成人蜜桃| 亚洲卡通欧美制服中文| av成人免费在线| 香蕉乱码成人久久天堂爱免费 | 久久久久国产一区二区| 麻豆成人在线| 欧美剧在线观看| 欧美色精品天天在线观看视频| 国产精品久久久久天堂| 国产一区二区三区免费观看| 在线日韩电影| 久久精品五月| 欧美a级理论片| 欧美日韩国产综合一区二区| 亚洲午夜影视影院在线观看| 亚洲一区二区网站| 久久精品国产999大香线蕉| 久久噜噜亚洲综合| 91久久线看在观草草青青| 亚洲午夜视频在线观看| 久久精品二区| 欧美激情第1页| 国产日韩精品在线观看| 伊人久久久大香线蕉综合直播 | 国产精品久久毛片a| 国产一区二区三区丝袜| 亚洲欧洲日本mm| 午夜久久影院| 亚洲国产美国国产综合一区二区| 亚洲午夜精品| 免费成人高清在线视频| 国产精品蜜臀在线观看| 亚洲精品1区| 久久久久一区二区三区四区| 日韩亚洲在线观看| 麻豆精品网站| 国产色综合久久| 亚洲一区免费在线观看|