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

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) 評(píng)論(0)  編輯 收藏 引用 所屬分類: OS
<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用鏈接

留言簿(10)

隨筆分類(307)

隨筆檔案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感覺這個(gè)博客還是不錯(cuò),雖然做的東西和我不大相關(guān),覺得看看還是有好處的

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

搜索

  •  

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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黑人| 国语自产精品视频在线看8查询8| 亚洲深夜福利在线| 欧美黄在线观看| 亚洲美女在线看| 免费成人在线观看视频| 国产欧美精品一区二区色综合| 亚洲乱码日产精品bd| 久久只精品国产| 亚洲自拍偷拍网址| 欧美日韩亚洲一区二| 亚洲欧洲日夜超级视频| 久久精彩视频| 亚洲天天影视| 欧美另类99xxxxx| **性色生活片久久毛片| 欧美一级播放| 亚洲无限乱码一二三四麻| 欧美看片网站| 亚洲人成网站影音先锋播放| 久久国产天堂福利天堂| 亚洲免费综合| 伊大人香蕉综合8在线视| 亚洲国产99精品国自产| 欧美精品一区二区久久婷婷| 亚洲淫性视频| 久久精品视频va| 亚洲乱码国产乱码精品精可以看| 99国产精品久久久久老师| 国产精一区二区三区| 久久人人97超碰国产公开结果| 久久人人爽爽爽人久久久| 99re66热这里只有精品4| 亚洲香蕉网站| 亚洲国产精品女人久久久| 91久久精品国产91久久| 国产精品一区免费在线观看| 久久久久久久一区| 欧美韩日亚洲| 久久精品人人做人人综合| 免费视频一区| 久久精品国产免费观看| 欧美精品在线视频| 久久九九精品99国产精品| 欧美激情精品久久久久| 久久久www成人免费精品| 欧美日韩不卡| 麻豆精品一区二区av白丝在线| 欧美日韩亚洲一区二区三区| 久久综合伊人77777麻豆| 欧美日韩在线播放一区二区| 久久在线免费视频| 国产精品亚洲不卡a| 亚洲欧洲午夜| 在线不卡中文字幕| 亚洲女同精品视频| 亚洲最新视频在线| 久久国产主播| 欧美一级播放| 国产精品高潮在线| 亚洲精品一区二区三| 一区二区三区在线视频免费观看| 亚洲天堂av电影| 一本久道久久综合狠狠爱| 久久久亚洲午夜电影| 午夜精品理论片| 欧美日本三级| 亚洲国产一区二区三区在线播| 伊大人香蕉综合8在线视| 亚洲欧美日韩国产综合| 亚洲主播在线| 欧美日韩精品伦理作品在线免费观看 | 久久综合精品一区| 欧美中在线观看| 国产精品成人观看视频免费| 亚洲人成77777在线观看网| 依依成人综合视频| 久久精品99久久香蕉国产色戒| 午夜精品美女久久久久av福利| 欧美日韩午夜视频在线观看| 最新国产拍偷乱拍精品| 亚洲国内高清视频| 欧美成人精品三级在线观看| 欧美国产精品v| 亚洲国产精品电影| 老鸭窝毛片一区二区三区| 欧美不卡一卡二卡免费版| 国语自产精品视频在线看| 亚洲欧美一区二区精品久久久| 午夜精品久久久久久久99樱桃| 国产精品毛片高清在线完整版| 一区二区三区视频在线| 亚洲在线不卡| 国产欧美精品在线观看| 久久er精品视频| 久久免费视频网站| 亚洲高清免费在线| 欧美理论大片| 一区二区三区色| 久久久精品2019中文字幕神马| 国产一区二区三区高清播放| 日韩亚洲欧美在线观看| 亚洲伊人一本大道中文字幕| 国产精品一区免费观看| 久久精品亚洲精品国产欧美kt∨| 麻豆精品在线播放| 中日韩视频在线观看| 国产精品永久在线| 久久综合五月天婷婷伊人| 91久久精品国产91久久性色tv| 亚洲一区二区三区久久| 国产亚洲精品bv在线观看| 久久―日本道色综合久久| 亚洲精品美女| 久久精品二区| 亚洲精品免费网站| 国产精品国产三级国产| 欧美一区二区三区在| 欧美电影在线免费观看网站| 亚洲午夜在线视频| 狠狠色噜噜狠狠色综合久 | 亚洲香蕉网站| 国产一区二区三区在线观看免费| 美女网站在线免费欧美精品| 一区二区三区日韩欧美| 久久精品一区二区| 日韩一区二区久久| 国产一区二区av| 欧美精品一区二区三区一线天视频 | 狠狠色狠狠色综合日日小说| 欧美激情自拍| 久久狠狠亚洲综合| 亚洲伦理在线| 美脚丝袜一区二区三区在线观看 | 午夜国产精品视频| 亚洲国产欧美一区二区三区同亚洲| 国产精品高清在线观看| 欧美www视频在线观看| 亚洲欧美中文另类| 亚洲人体1000| 欧美国产激情| 麻豆av一区二区三区久久| 午夜在线精品| 亚洲影音一区| 亚洲免费观看高清在线观看 | 亚洲午夜精品久久久久久app| 免费黄网站欧美| 西西裸体人体做爰大胆久久久| 亚洲精品国产精品国自产观看浪潮| 国产麻豆日韩欧美久久| 欧美日韩一卡| 欧美激情国产精品| 美女主播视频一区| 久久午夜电影网| 久久aⅴ国产欧美74aaa| 亚洲一区二区四区| 日韩网站在线| 91久久夜色精品国产网站| 蜜桃伊人久久| 久久综合中文| 美日韩精品视频免费看| 久久久久高清| 久久精品国产一区二区三区免费看| 午夜精品国产| 亚洲一区二区三区777| 在线视频精品一区| 夜夜爽av福利精品导航| 亚洲伦理自拍| 99pao成人国产永久免费视频| 亚洲美女视频在线观看| 亚洲日本精品国产第一区| 亚洲国语精品自产拍在线观看| 精品成人a区在线观看| 一色屋精品亚洲香蕉网站| 亚洲第一在线| 亚洲精品在线看| 中文国产成人精品| 亚洲一区二区免费在线| 亚洲综合久久久久| 亚洲欧洲99久久| 久久精品夜色噜噜亚洲aⅴ| 久久全球大尺度高清视频| 噜噜噜久久亚洲精品国产品小说| 美女视频黄a大片欧美| 免费观看成人鲁鲁鲁鲁鲁视频| 欧美国产一区二区在线观看 | 国产精品久久久久久福利一牛影视| 欧美色欧美亚洲高清在线视频| 国产精品久久久久久av下载红粉 | 这里只有精品电影| 亚洲欧美另类在线观看| 久久精品视频va| 欧美成人综合在线| 亚洲美女视频在线观看| 午夜精品久久99蜜桃的功能介绍|