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

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
<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用鏈接

留言簿(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

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            aa亚洲婷婷| 欧美丝袜一区二区| 91久久国产综合久久| 久久久人成影片一区二区三区| aa级大片欧美| 午夜在线电影亚洲一区| 久久精品人人做人人爽| 欧美成年人网| 91久久嫩草影院一区二区| av成人天堂| 欧美一区网站| 欧美二区在线观看| 亚洲风情在线资源站| 伊人影院久久| 亚洲精品麻豆| 亚洲欧美日韩在线| 奶水喷射视频一区| 一本色道久久88综合日韩精品| 亚洲综合视频在线| 久久综合亚洲社区| 国产精品无码永久免费888| 在线观看欧美亚洲| 亚洲免费影视| 亚洲电影在线| 欧美在线观看视频在线| 欧美片第一页| 一区二区亚洲| 欧美一级大片在线观看| 亚洲福利视频三区| 欧美亚洲视频| 欧美色精品天天在线观看视频 | 免费亚洲电影在线| 99精品视频一区| 美女国产精品| 国产一区二区三区自拍 | 欧美成人综合网站| 亚洲午夜黄色| 欧美激情第六页| 国内精品美女在线观看| 亚洲影视在线| 日韩一级免费| 欧美精品18| 亚洲欧洲一区| 牛牛国产精品| 久久久久久久久久码影片| 国产精品爽爽爽| 日韩午夜高潮| 亚洲成人在线网站| 久久这里只有| 激情六月婷婷综合| 欧美一区二区精美| 亚洲色诱最新| 欧美婷婷久久| 亚洲一二三区精品| 亚洲六月丁香色婷婷综合久久| 免费黄网站欧美| 91久久夜色精品国产网站| 裸体丰满少妇做受久久99精品| 性欧美video另类hd性玩具| 国产精品腿扒开做爽爽爽挤奶网站| 一本高清dvd不卡在线观看| 亚洲黄一区二区三区| 欧美va天堂在线| 91久久精品国产| 欧美顶级大胆免费视频| 嫩草影视亚洲| av成人免费在线| 一区二区欧美在线| 极品尤物av久久免费看| 久久在线播放| 欧美综合国产| 国产精品久久久久久久第一福利| 欧美刺激午夜性久久久久久久| 欧美日韩综合在线| 亚洲精品久久久久久下一站| 在线精品国产成人综合| 免费一级欧美在线大片| 亚洲人在线视频| 中文日韩在线| 欧美午夜不卡在线观看免费| 亚洲伊人伊色伊影伊综合网| 欧美影院在线| 亚洲第一福利视频| 欧美视频在线观看免费| 欧美在线3区| 一本色道久久综合精品竹菊| 久久国产综合精品| 日韩系列欧美系列| 国产欧美va欧美不卡在线| 美女尤物久久精品| 亚洲天堂成人在线视频| 久久国产精品免费一区| 亚洲精品中文字幕女同| 国产亚洲人成网站在线观看| 欧美日韩不卡合集视频| 亚洲欧美日韩系列| 日韩一区二区久久| 亚洲国产一区二区三区高清 | 欧美午夜一区二区福利视频| 亚洲欧美乱综合| 亚洲美女在线国产| 亚洲缚视频在线观看| 欧美成人tv| 欧美午夜精品久久久久久孕妇 | 欧美精品一区二区在线观看 | 一区二区三区视频在线看| 欧美不卡视频| 欧美成人免费网站| 鲁鲁狠狠狠7777一区二区| 亚洲欧美日韩国产另类专区| 夜夜嗨av一区二区三区中文字幕| 91久久国产综合久久| 亚洲精品小视频| 夜夜嗨av一区二区三区| 在线综合亚洲| 久久精品久久综合| 免费在线观看日韩欧美| 欧美国产日韩视频| 国产精品每日更新| 欲香欲色天天天综合和网| 一区二区三区av| 久久久999精品| 亚洲国产女人aaa毛片在线| 日韩视频在线观看国产| 午夜日韩在线观看| 欧美激情视频一区二区三区免费| 国产精品久久激情| 亚洲人成网站在线观看播放| 亚洲综合精品自拍| 欧美激情一区二区三区在线视频| 一本色道久久综合精品竹菊| 久久夜色精品亚洲噜噜国产mv| 欧美日韩国产天堂| 99国产精品久久久久老师| 久久免费视频在线观看| 中文精品在线| 国产精品高潮粉嫩av| 亚洲精品视频二区| 久久午夜视频| 欧美在线免费视屏| 亚洲一级二级| 亚洲欧洲免费视频| 久久这里有精品视频| 在线观看三级视频欧美| 久久精品视频va| 西西裸体人体做爰大胆久久久| 免费在线欧美黄色| 亚洲毛片播放| 亚洲精品美女久久7777777| 蜜臀99久久精品久久久久久软件| 国产自产女人91一区在线观看| 亚洲在线免费| 香蕉久久夜色| 91久久在线视频| 亚洲国产精品久久91精品| 欧美—级高清免费播放| 一本色道久久88综合亚洲精品ⅰ| 一本色道久久88精品综合| 国产精品视频免费观看www| 奶水喷射视频一区| 欧美第一黄色网| 亚洲欧美日韩专区| 久久五月婷婷丁香社区| 亚洲一区二区精品| 欧美在线影院在线视频| 日韩一级二级三级| 午夜精品视频一区| 亚洲片国产一区一级在线观看| 中日韩美女免费视频网址在线观看| 国产亚洲成精品久久| 亚洲黄色一区二区三区| 国产日韩欧美日韩| 亚洲图片欧美一区| 一本一本a久久| 欧美福利视频| 欧美www视频在线观看| 国产欧美精品va在线观看| 亚洲裸体在线观看| 亚洲高清资源综合久久精品| 午夜精品在线看| 午夜精品久久久久久久蜜桃app| 久久久久久一区二区| 美女脱光内衣内裤视频久久网站| 国产精品成人v| 午夜精品久久99蜜桃的功能介绍| 最新亚洲一区| 亚洲福利视频网站| 黄色av一区| 麻豆精品一区二区av白丝在线| 久久综合久久综合九色| 国产字幕视频一区二区| 久久久久久久综合狠狠综合| 久久av免费一区| 伊人久久婷婷色综合98网| 久久综合狠狠综合久久综青草 | 国产日韩精品一区二区浪潮av| 一区二区三区欧美亚洲| 久久精品一区蜜桃臀影院| 韩国女主播一区| 欧美精品在线免费|