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

posts - 297,  comments - 15,  trackbacks - 0

Critical section

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In concurrent programming a critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution. A critical section will usually terminate in fixed time, and a thread, task or process will only have to wait a fixed time to enter it (i.e. bounded waiting). Some synchronization mechanism is required at the entry and exit of the critical section to ensure exclusive use, for example a semaphore.

By carefully controlling which variables are modified inside and outside the critical section (usually, by accessing important state only from within), concurrent access to that state is prevented. A critical section is typically used when a multithreaded program must update multiple related variables without a separate thread making conflicting changes to that data. In a related situation, a critical section may be used to ensure a shared resource, for example a printer, can only be accessed by one process at a time.

How critical sections are implemented varies among operating systems.

The simplest method is to prevent any change of processor control inside the critical section. On uni-processor systems, this can be done by disabling interrupts on entry into the critical section, avoiding system calls that can cause a context switch while inside the section and restoring interrupts to their previous state on exit. Any thread of execution entering any critical section anywhere in the system will, with this implementation, prevent any other thread, including an interrupt, from getting the CPU and therefore from entering any other critical section or, indeed, any code whatsoever, until the original thread leaves its critical section.

This brute-force approach can be improved upon by using semaphores. To enter a critical section, a thread must obtain a semaphore, which it releases on leaving the section. Other threads are prevented from entering the critical section at the same time as the original thread, but are free to gain control of the CPU and execute other code, including other critical sections that are protected by different semaphores.

Some confusion exists in the literature about the relationship between different critical sections in the same program.[citation needed] In general, a resource that must be protected from concurrent access may be accessed by several pieces of code. Each piece must be guarded by a common semaphore. Is each piece now a critical section or are all the pieces guarded by the same semaphore in aggregate a single critical section? This confusion is evident in definitions of a critical section such as "... a piece of code that can only be executed by one process or thread at a time". This only works if all access to a protected resource is contained in one "piece of code", which requires either the definition of a piece of code or the code itself to be somewhat contrived.

Contents

[hide]

[edit] Application Level Critical Sections

Application-level critical sections reside in the memory range of the process and are usually modifiable by the process itself. This is called a user-space object because the program run by the user (as opposed to the kernel) can modify and interact with the object. However the functions called may jump to kernel-space code to register the user-space object with the kernel.

Example Code For Critical Sections with POSIX pthread library

/* Sample C/C++, Unix/Linux */
#include <pthread.h>

/* This is the critical section object (statically allocated). */
static pthread_mutex_t cs_mutex = PTHREAD_MUTEX_INITIALIZER;

void f()
{
/* Enter the critical section -- other threads are locked out */
pthread_mutex_lock( &cs_mutex );

/* Do some thread-safe processing! */

/*Leave the critical section -- other threads can now pthread_mutex_lock() */
pthread_mutex_unlock( &cs_mutex );
}

Example Code For Critical Sections with Win32 API

/* Sample C/C++, Windows, link to kernel32.dll */
#include <windows.h>

static CRITICAL_SECTION cs; /* This is the critical section object -- once initialized,
it cannot be moved in memory */

/* If you program in OOP, declare this in your class */

/* Initialize the critical section before entering multi-threaded context. */
InitializeCriticalSection(&cs);

void f()
{
/* Enter the critical section -- other threads are locked out */
EnterCriticalSection(&cs);

/* Do some thread-safe processing! */

/* Leave the critical section -- other threads can now EnterCriticalSection() */
LeaveCriticalSection(&cs);
}

/* Release system object when all finished -- usually at the end of the cleanup code */
DeleteCriticalSection(&cs);

Note that on Windows NT (not 9x/ME), the function TryEnterCriticalSection() can be used to attempt to enter the critical section. This function returns immediately so that the thread can do other things if it fails to enter the critical section (usually due to another thread having locked it). With the pthreads library, the equivalent function is pthread_mutex_trylock(). Note that the use of a CriticalSection is not the same as a Win32 Mutex, which is an object used for inter-process synchronization. A Win32 CriticalSection is for intra-process synchronization (and is much faster as far as lock times), however it cannot be shared across processes.

[edit] Kernel Level Critical Sections

Typically, critical sections prevent process and thread migration between processors and the preemption of processes and threads by interrupts and other processes and threads.

Critical sections often allow nesting. Nesting allows multiple critical sections to be entered and exited at little cost.

If the scheduler interrupts the current process or thread in a critical section, the scheduler will either allow the process or thread to run to completion of the critical section, or it will schedule the process or thread for another complete quantum. The scheduler will not migrate the process or thread to another processor, and it will not schedule another process or thread to run while the current process or thread is in a critical section.

Similarly, if an interrupt occurs in a critical section, the interrupt's information is recorded for future processing, and execution is returned to the process or thread in the critical section. Once the critical section is exited, and in some cases the scheduled quantum completes, the pending interrupt will be executed.

Since critical sections may execute only on the processor on which they are entered, synchronization is only required within the executing processor. This allows critical sections to be entered and exited at almost zero cost. No interprocessor synchronization is required, only instruction stream synchronization. Most processors provide the required amount of synchronization by the simple act of interrupting the current execution state. This allows critical sections in most cases to be nothing more than a per processor count of critical sections entered.

Performance enhancements include executing pending interrupts at the exit of all critical sections and allowing the scheduler to run at the exit of all critical sections. Furthermore, pending interrupts may be transferred to other processors for execution.

Critical sections should not be used as a long-lived locking primitive. They should be short enough that the critical section will be entered, executed, and exited without any interrupts occurring, from neither hardware much less the scheduler.

Kernel Level Critical Sections are the base of the software lockout issue.

[edit] See also

[edit] External links

Critical Section documentation on the MSDN Library homepage: http://msdn2.microsoft.com/en-us/library/ms682530.aspx

posted on 2009-05-11 11:48 chatler 閱讀(1256) 評論(0)  編輯 收藏 引用 所屬分類: OS
<2010年10月>
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>
            久久久999国产| 国产综合视频| 亚洲欧美乱综合| 99re国产精品| 99国产精品99久久久久久粉嫩| 欧美成人黑人xx视频免费观看| 久久久www免费人成黑人精品 | 亚洲视频免费| 亚洲深夜福利视频| 亚洲一区二区三区高清不卡| 亚洲一区二区三区高清 | 91久久精品一区二区三区| 在线不卡中文字幕播放| 亚洲国产精品99久久久久久久久| 亚洲国产你懂的| 亚洲免费观看高清完整版在线观看| 亚洲精选一区| 亚洲欧洲av一区二区三区久久| 欧美在线不卡| 欧美福利专区| 99热这里只有精品8| 午夜视频久久久| 免费成人性网站| 欧美网站在线| 尤物精品在线| 亚洲一区久久久| 久久亚洲国产精品一区二区| 91久久精品网| 国产精品99久久不卡二区| 夜夜狂射影院欧美极品| 欧美在线观看一区二区| 久久夜色精品国产亚洲aⅴ| 欧美激情国产高清| 亚洲视频一区二区免费在线观看| 久久久99国产精品免费| 欧美日韩国产麻豆| 一区在线影院| 亚洲综合国产激情另类一区| 欧美va亚洲va日韩∨a综合色| 日韩视频第一页| 久久久久亚洲综合| 国产精品理论片在线观看| 影院欧美亚洲| 久久激情视频| 亚洲欧美日韩精品久久久久| 欧美成人精品不卡视频在线观看| 亚洲性感美女99在线| 免费不卡在线观看av| 国产欧美日韩综合一区在线观看 | 亚洲图片欧美一区| 免费欧美网站| 欧美一级午夜免费电影| 欧美色精品天天在线观看视频 | 国内外成人免费激情在线视频| 亚洲午夜激情在线| 欧美激情视频在线播放| 欧美一区精品| 国产日韩欧美一区在线| 亚洲大片一区二区三区| 久久激情视频久久| 国产亚洲欧美一区在线观看| 亚洲欧美中文在线视频| 中文在线资源观看视频网站免费不卡| 欧美激情第8页| 亚洲精品欧美极品| 亚洲作爱视频| 亚洲精品专区| 欧美三日本三级三级在线播放| 亚洲精品在线免费观看视频| 免费影视亚洲| 久久久国产精品亚洲一区| 国产日韩欧美在线播放| 久久激情视频久久| 欧美专区亚洲专区| 韩国一区二区三区美女美女秀| 欧美一区三区三区高中清蜜桃| 在线视频精品一区| 国产精品久线观看视频| 欧美在线亚洲在线| 久久精品成人欧美大片古装| 黄网站色欧美视频| 欧美成人国产va精品日本一级| 麻豆9191精品国产| 99re在线精品| 亚洲一区视频| 亚洲第一精品夜夜躁人人爽| 在线看一区二区| 亚洲电影免费在线观看| 欧美成人中文字幕| 一区二区三区视频在线观看| aa级大片欧美| 国产亚洲日本欧美韩国| 免播放器亚洲一区| 欧美精品系列| 久久精品91久久久久久再现| 久热综合在线亚洲精品| 99视频精品在线| 夜夜精品视频一区二区| 一区二区三区回区在观看免费视频 | 亚洲国产精品成人综合| 欧美激情第1页| 午夜精品久久久久久99热| 午夜精品久久久久久久蜜桃app| 影音国产精品| 国产精品99久久久久久白浆小说 | 蘑菇福利视频一区播放| 亚洲综合色激情五月| 久久精品30| av成人免费在线观看| 羞羞答答国产精品www一本| 一区二区三区在线免费观看| 亚洲乱码一区二区| 一区免费视频| 亚洲一区亚洲| 99精品视频免费全部在线| 欧美在线免费一级片| 亚洲视频精选| 欧美高清视频www夜色资源网| 久久av二区| 欧美日韩在线影院| 欧美顶级艳妇交换群宴| 国产欧美日韩专区发布| 一区二区高清在线| 亚洲日本成人在线观看| 久久激情综合| 欧美一区二区精品在线| 欧美人与性动交α欧美精品济南到| 久久精品观看| 国产精品一区亚洲| 99精品福利视频| 99精品国产99久久久久久福利| 久久精品一区四区| 久久一区二区三区四区| 国产一区二区精品| 亚洲在线中文字幕| 亚洲一区二区三区四区中文| 欧美日韩成人网| 日韩午夜激情电影| 99精品欧美一区二区三区综合在线 | 亚洲高清不卡| 亚洲国产精品一区二区www| 亚洲欧美日韩精品| 欧美一级理论片| 午夜视频在线观看一区| 欧美一区二区三区四区在线| 欧美激情按摩| 亚洲第一福利社区| 亚洲精品偷拍| 欧美日韩国产麻豆| 一片黄亚洲嫩模| 激情综合网址| 一区二区三区免费网站| 欧美亚州一区二区三区 | 欧美成人四级电影| 欧美在线你懂的| 欧美日韩一级黄| 亚洲日本成人| 亚洲欧洲在线观看| 欧美日韩另类综合| 亚洲国产成人在线视频| 黄色综合网站| 久久免费视频在线观看| 亚洲国产裸拍裸体视频在线观看乱了| 黑人一区二区| 欧美在线视频导航| 久久久久久久网站| 91久久中文| 韩国欧美国产1区| 欧美成人资源| 久久久噜噜噜久噜久久| 久久久久久久波多野高潮日日| 午夜精品理论片| 久久青青草综合| 亚洲激情视频| 一区二区三区你懂的| 欧美激情导航| 在线综合欧美| 久久久九九九九| 亚洲第一福利社区| 欧美美女操人视频| 一区二区日韩免费看| 亚洲精品之草原avav久久| 欧美日韩国产欧美日美国产精品| 亚洲美女在线视频| 欧美影片第一页| 国产中文一区二区三区| 欧美影院成年免费版| 在线亚洲欧美视频| 欧美电影在线免费观看网站| 99riav1国产精品视频| 在线免费观看日本欧美| 麻豆国产精品va在线观看不卡| 亚洲第一黄色| 亚洲综合社区| 亚洲麻豆一区| 国产麻豆精品theporn| 久久综合久久久久88| 一区二区欧美在线| 亚洲国产成人精品女人久久久 | 国产日韩精品视频一区|