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

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 閱讀(556) 評論(0)  編輯 收藏 引用 所屬分類: OS
<2010年7月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用鏈接

留言簿(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>
            欧美一区二区在线看| 欧美激情一区二区三区成人| 欧美激情视频一区二区三区免费| 亚洲欧美日韩区| 亚洲无毛电影| 欧美一区二区精品| 亚洲国产成人久久综合| 欧美亚洲日本一区| 欧美在线日韩在线| 开心色5月久久精品| 麻豆精品精品国产自在97香蕉| 久久精品电影| 亚洲国产精品一区二区尤物区| 最近看过的日韩成人| 亚洲精品一区在线观看| 在线一区二区视频| 欧美在线综合| 亚洲国产天堂久久国产91| 一区二区三区福利| 欧美一区久久| 欧美人成免费网站| 国产欧美日韩一区二区三区在线观看| 韩国av一区| 日韩亚洲在线观看| 欧美在线视频免费播放| 免费欧美高清视频| 日韩视频永久免费观看| 欧美一级午夜免费电影| 免费成人黄色| 国产婷婷97碰碰久久人人蜜臀| 亚洲国产婷婷香蕉久久久久久| 亚洲国产国产亚洲一二三| 日韩一级精品视频在线观看| 午夜精品视频| 亚洲精品五月天| 久久精品国产第一区二区三区| 欧美日韩成人网| 亚洲第一狼人社区| 久久国产毛片| 一本色道久久综合亚洲精品高清 | 午夜在线a亚洲v天堂网2018| 久久中文字幕一区| 亚洲制服少妇| 欧美日韩国产一区| 91久久精品美女| 久久深夜福利免费观看| 亚洲一二三区视频在线观看| 欧美国产日本韩| 亚洲欧美日韩中文播放| 男女精品网站| 久久精品国产一区二区三区免费看| 欧美性猛交xxxx乱大交退制版| 亚洲人成77777在线观看网| 久久久99国产精品免费| 亚洲嫩草精品久久| 国产精品色一区二区三区| 亚洲一区二区三区乱码aⅴ蜜桃女 亚洲一区二区三区乱码aⅴ | 开元免费观看欧美电视剧网站| 亚洲国产午夜| 国产精品日本| 亚洲午夜日本在线观看| 亚洲精品日韩综合观看成人91| 免费国产自线拍一欧美视频| 亚洲综合社区| 国产欧美日韩中文字幕在线| 亚洲欧美日韩直播| 亚洲永久免费观看| 国产一区成人| 欧美+日本+国产+在线a∨观看| 久久久久国产精品一区三寸| 伊人成人开心激情综合网| 欧美国产日韩一二三区| 欧美精品乱码久久久久久按摩| 亚洲已满18点击进入久久| 夜夜嗨av色综合久久久综合网| 欧美日韩精品一区二区三区| 亚洲一区二区免费看| 亚洲欧美另类国产| 亚洲第一网站| 亚洲美女av黄| 国产一区二区av| 欧美国产第一页| 欧美精品一区在线观看| 亚洲永久免费观看| 久久精品天堂| 99国产精品久久久久老师 | 性色一区二区| 久久蜜桃香蕉精品一区二区三区| 亚洲高清不卡在线观看| 亚洲久久一区| 欧美一区二区三区视频免费播放 | 国产主播在线一区| 亚洲国产成人久久| 国产精品每日更新| 欧美aⅴ99久久黑人专区| 欧美激情四色| 欧美中文字幕在线观看| 久久综合精品一区| 亚洲一区激情| 黄色影院成人| 久久综合久色欧美综合狠狠 | 国产精品视频一二三| 久久婷婷综合激情| 欧美日韩国产综合网| 久久精品人人做人人爽| 欧美大片免费看| 久久精品一级爱片| 欧美欧美在线| 男男成人高潮片免费网站| 国产精品国产精品| 亚洲第一页在线| 国产视频精品va久久久久久| 国产欧美亚洲日本| 国产日韩欧美日韩大片| 欧美成人黑人xx视频免费观看| 亚洲福利视频一区| 国产精品久久久久久户外露出| 免费成人毛片| 国产精品一香蕉国产线看观看 | 女女同性女同一区二区三区91| 免费观看日韩av| 欧美在线观看一二区| 欧美日韩一区三区四区| 欧美高清视频| 亚洲午夜激情网站| 欧美在线三区| 欧美一级二级三级蜜桃| 欧美日韩国产色综合一二三四| 另类天堂视频在线观看| 国产麻豆午夜三级精品| 一本大道久久精品懂色aⅴ| 亚洲精品裸体| 快she精品国产999| 麻豆精品精华液| 在线精品一区| 久久综合伊人77777| 老司机成人网| 在线日韩欧美| 欧美不卡在线视频| 亚洲电影免费观看高清完整版| 国产在线麻豆精品观看| 久久xxxx| 欧美成人精品三级在线观看| 久久久国产一区二区| 久久久久久日产精品| 狠狠干成人综合网| 久久午夜电影网| 欧美激情精品久久久久久| 亚洲第一综合天堂另类专| 久久国产精品一区二区三区| 麻豆精品视频在线观看视频| 亚洲国产高清一区二区三区| 免费欧美视频| 亚洲日本va在线观看| 国产精品99久久久久久久久久久久| 欧美精品一区二区三区很污很色的| 久久九九国产精品| 午夜精品在线看| 久久综合久久综合九色| avtt综合网| 国产精品自在线| 久久久久久久久久码影片| 欧美福利一区二区| 亚洲一区二区三区精品在线| 国产日韩在线看| 美女网站久久| 在线综合亚洲| 免费在线观看一区二区| 一本色道久久综合亚洲精品小说 | 久久琪琪电影院| 亚洲欧洲中文日韩久久av乱码| 一本一本久久a久久精品综合妖精| 欧美日韩国产高清| 亚洲一区二区三区中文字幕| 久久免费视频这里只有精品| 久久亚洲电影| 夜夜嗨av一区二区三区四区| 久久成人18免费观看| 亚洲黄色av一区| 国产精品区免费视频| 久久久久成人精品免费播放动漫| 最新日韩在线视频| 久久精品视频在线播放| 一区二区三区精品国产| 激情欧美国产欧美| 国产精品观看| 欧美激情第六页| 久久激情网站| 亚洲一区综合| 羞羞色国产精品| 亚洲精品国产无天堂网2021| 欧美综合国产精品久久丁香| 亚洲另类一区二区| 最新高清无码专区| 国产精品久久综合| 欧美激情视频在线免费观看 欧美视频免费一| 亚洲欧美精品在线| 亚洲一区综合| 亚洲制服少妇| 亚洲摸下面视频|