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

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 閱讀(1261) 評論(0)  編輯 收藏 引用 所屬分類: OS
<2010年6月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用鏈接

留言簿(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>
            亚洲视频在线观看三级| 亚洲国产精品热久久| 亚洲乱码国产乱码精品精天堂| 久久精品亚洲乱码伦伦中文 | 亚洲欧洲美洲综合色网| 欧美成人一区二区三区| 亚洲精品乱码视频 | 一区二区三区欧美亚洲| 亚洲激情影视| 欧美色播在线播放| 亚洲一区二区免费在线| 亚洲主播在线| 国产一区再线| 欧美va亚洲va日韩∨a综合色| 久久精品2019中文字幕| 亚洲高清久久久| 亚洲国产清纯| 国产精品视区| 久久亚洲视频| 欧美精品激情在线观看| 亚洲综合色激情五月| 亚洲一区二区三区三| 激情综合在线| 亚洲精品精选| 国产一区欧美| 亚洲精品乱码久久久久久日本蜜臀 | 久久久国产成人精品| 久久米奇亚洲| 一本色道久久| 久久久久久久999| 日韩午夜电影av| 亚洲欧美在线免费观看| 亚洲国产一二三| 一区二区三区久久精品| 一区免费视频| 亚洲视频 欧洲视频| 亚洲国产精品va在线看黑人| 一区二区三区视频在线| 伊人春色精品| 亚洲一区激情| 一本色道久久88亚洲综合88| 久久成人在线| 亚洲一区二区av电影| 另类亚洲自拍| 欧美在线综合视频| 欧美日韩在线大尺度| 欧美激情国产高清| 国内精品写真在线观看| 亚洲一区二区成人| 一区二区三区日韩精品| 欧美sm视频| 两个人的视频www国产精品| 欧美视频成人| 亚洲激情网站免费观看| 在线成人h网| 午夜免费日韩视频| 性亚洲最疯狂xxxx高清| 欧美视频在线看| 亚洲精品孕妇| 夜夜嗨av一区二区三区免费区| 久久久久国产成人精品亚洲午夜| 午夜亚洲激情| 国产精品区一区| 亚洲午夜国产成人av电影男同| 一本色道久久加勒比88综合| 蜜桃久久av| 欧美国产激情二区三区| 亚洲国产一区二区在线| 久久一区精品| 欧美二区在线播放| 亚洲国产欧美日韩| 蜜桃久久精品乱码一区二区| 欧美激情精品久久久久久久变态| 在线欧美不卡| 欧美成人精品一区二区三区| 亚洲国产精品久久久久婷婷884| 在线看片欧美| 欧美国产日韩在线| 亚洲精品视频在线| 午夜精品剧场| 国产综合第一页| 久久露脸国产精品| 亚洲二区精品| 一区二区三区日韩精品视频| 国产精品激情偷乱一区二区∴| 一区二区三区福利| 欧美怡红院视频| 经典三级久久| 免费人成精品欧美精品| 亚洲精品中文字幕女同| 亚洲欧洲av一区二区| 国产一区二区在线观看免费播放| 欧美在线视频不卡| 亚洲电影免费观看高清| 亚洲视频在线观看| 国产亚洲精品bt天堂精选| 久久亚洲精品视频| 一本色道久久综合亚洲精品按摩| 欧美一区二区在线免费播放| 国产一区在线播放| 欧美极品在线观看| 亚洲欧美三级在线| 欧美风情在线观看| 亚洲欧美制服中文字幕| 在线免费精品视频| 国产精品久久久久久久久 | 欧美成人综合一区| 在线一区二区日韩| 国产一区二区三区四区三区四| 你懂的国产精品| 亚洲欧美日韩精品一区二区| 欧美国产日产韩国视频| 午夜精品av| 日韩小视频在线观看| 国模精品娜娜一二三区| 欧美精品一区二区三区很污很色的| 亚洲欧美精品| 亚洲日韩欧美一区二区在线| 久久精品女人| 亚洲一区欧美一区| 亚洲国产精品久久久| 国产视频在线观看一区二区三区| 欧美韩国日本综合| 久久久美女艺术照精彩视频福利播放 | 亚洲第一精品影视| 国产精品视频网| 欧美日韩成人一区二区| 久久久久久噜噜噜久久久精品| 亚洲图片自拍偷拍| 亚洲欧洲在线播放| 麻豆成人综合网| 欧美一区二区| 亚洲免费视频网站| 中日韩美女免费视频网址在线观看| 尤物网精品视频| 国模大胆一区二区三区| 国产欧美高清| 国产精品久久久久婷婷| 欧美日韩一区二区三区四区在线观看| 久久一区免费| 另类图片国产| 久久综合伊人77777蜜臀| 久久精品视频播放| 久久激情久久| 久久精品国产免费看久久精品| 欧美一区二区三区视频在线观看| 亚洲一区在线免费观看| 亚洲视频视频在线| 亚洲视频欧美视频| 亚洲性感激情| 午夜性色一区二区三区免费视频| 亚洲午夜激情免费视频| 亚洲视频999| 亚洲欧美国产毛片在线| 亚洲一级黄色av| 亚洲欧美日韩国产另类专区| 亚洲欧美日本视频在线观看| 午夜久久影院| 久久久久久夜精品精品免费| 久久久青草婷婷精品综合日韩| 久久免费精品视频| 免费看精品久久片| 欧美日韩国产一区精品一区| 欧美亚洲第一区| 欧美www视频| 久久午夜精品| 久热这里只精品99re8久| 麻豆精品精华液| 亚洲理伦在线| 亚洲狼人精品一区二区三区| 99亚洲视频| 玖玖玖国产精品| 亚洲综合不卡| 欧美日韩在线视频一区二区| 亚洲国产一区二区三区青草影视| 久久精品亚洲一区二区三区浴池| 日韩视频免费| 欧美高清在线视频| 一色屋精品视频在线看| 久久精品论坛| 午夜欧美精品久久久久久久| 国产精品成人在线| 亚洲无毛电影| 亚洲精品一区在线| 欧美日韩成人综合天天影院| 亚洲免费播放| 亚洲福利专区| 久久手机精品视频| 在线播放不卡| 蜜桃久久av一区| 久久网站免费| 亚洲激情自拍| 欧美大片在线观看一区二区| 久久久xxx| 在线观看日韩www视频免费| 久久手机精品视频| 久久一日本道色综合久久| 在线看日韩av| 亚洲韩国青草视频| 欧美巨乳波霸|