Critical Section Object

Critical Section  Object  From MSDN

Critical Section Objects

A critical section object provides synchronization similar to that provided by a mutex object, except that a critical section can be used only by the threads of a single process.

Event, mutex, and semaphore objects can also be used in a single-process application,

but critical section objects provide a slightly faster, more efficient mechanism for mutual-exclusion synchronization (a processor-specific test and set instruction).

Like a mutex object, a critical section object can be owned by only one thread at a time, which makes it useful for protecting a shared resource from simultaneous access.

Unlike a mutex object, there is no way to tell whether a critical section has been abandoned.

Critical Section Object(临界区对象)提供一种同步机制.类似于mutex object. 但是Critical Section Object 对象仅仅只能在单进程中使用.

Event,Mutex  and semaphore object 也是只能在单进程程序中使用.

但是Critical Section Object 提供一种轻量级 ,快速,高效的机制来同步并发执行.

类型于Mutex Object ,Critical Section Object 同一时间仅仅能被一个线程拥有.对于共享资源的并发访问非常有用.

与Mutex Object 不同的是,不存在测试一个Critical Section Object 是否被抛弃.

Starting with Windows Server 2003 with Service Pack 1 (SP1), threads waiting on a critical section do not acquire the critical section on a first-come, first-serve basis.

This change increases performance significantly for most code.

However, some applications depend on first-in, first-out (FIFO) ordering and may perform poorly or not at all on current versions of Windows .

(for example, applications that have been using critical sections as a rate-limiter).

从Windows Server2003 开始,线程获取Critical Section Object 不需要先到先得,这个改变大大提高代码小龙.

然后,一些应用程序依赖先进先出FIFO顺序来执行效率很低.

To ensure that your code continues to work correctly, you may need to add an additional level of synchronization.

For example, suppose you have a producer thread and a consumer thread that are using a critical section object to synchronize their work.

Create two event objects, one for each thread to use to signal that it is ready for the other thread to proceed.

The consumer thread will wait for the producer to signal its event before entering the critical section, and the producer thread will wait for the consumer thread to signal its event before entering the critical section.

After each thread leaves the critical section, it signals its event to release the other thread.

Windows Server 2003 and Windows XP:

Threads that are waiting on a critical section are added to a wait queue;

they are woken and generally acquire the critical section in the order in which they were added to the queue.

However, if threads are added to this queue at a fast enough rate, performance can be degraded because of the time it takes to awaken each waiting thread.

在Windows Server2003 和Windows XP:

等待Critical Section Object 对象的线程都被加入到等待队列.

然后,线程被唤醒是按照在等待队列中的顺序进行.然而,如果现在被加入到队列中的速度足够快,在队伍中排队的时间远远小于线程被唤醒的时间.

测试代码:

#include <windows.h>
#include <iostream>
#include <vector>
using namespace std;

#define THREADCOUNT 9

CRITICAL_SECTION  testCS;
DWORD WINAPI ThreadFunc( LPVOID );
std::vector<int> orderVec;

//记录线程调用顺序
int main( void )
{
	HANDLE aThread[THREADCOUNT];
	DWORD ThreadID;
	int i;
	InitializeCriticalSection(&testCS);
	// Create worker threads

	for( i=0; i < THREADCOUNT; i++ )
	{
		aThread[i] = CreateThread(
			NULL,       // default security attributes
			0,          // default stack size
			ThreadFunc, //(LPTHREAD_START_ROUTINE)
			(LPVOID)i,       // no thread function arguments
			0,          // default creation flags
			&ThreadID); // receive thread identifier
	}

	Sleep(100);
	for( i=0; i < THREADCOUNT; i++ )
		CloseHandle(aThread[i]);

	int count = orderVec.size();
	cout << " order of calling thread:";
	for (int i=0; i<count; ++i)
	{
		cout << orderVec[i] << ", ";
	}
	DeleteCriticalSection(&testCS);
	system("pause");
	return 0;
}

DWORD WINAPI ThreadFunc( LPVOID lpParam )
{
	//所有访问orderVec 对象的线程,都将在Critical Section Object 按照FIFO顺序排队.
	//当不存在临界区时,orderVec顺序将乱序.
	EnterCriticalSection(&testCS);
	int i = (int)lpParam;
	orderVec.push_back(i);
	orderVec.push_back(i);
	orderVec.push_back(i);

	LeaveCriticalSection(&testCS);
	return TRUE;
}

测试结果:

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 08:55:23

Critical Section Object的相关文章

spinlock,mutex,semaphore,critical section的作用与差别

某年深信服的笔试题,考的就是多线程的同步.简单的解释下方便记忆: 1.spinlock:自旋锁.是专为防止多处理器并发而引入的一种锁. 2.mutex:相互排斥量. 仅仅有拥有相互排斥对象的线程才有訪问公共资源的权限.保证了资源不会同一时候被多个线程訪问. 3.semaphore:信号量.同意多个线程同一时候訪问资源,限制訪问资源的最大线程数. 4.critical section:临界区. 随意时刻仅仅同意一个线程对共享资源进行訪问.

windows下使用Critical Section和Mutex实现线程同步实例

利用critical section 和 Mutex两种不同的线程同步的方法实现生产者消费者问题.生产者线程要能够对一个计数器进行增的操作,并且将其输出在控制台上,消费者线程能够对这个计数器进行减的操作,并将其输出在控制台上.两种线程都共享一个计数器. 其中增.减计数器的数我设置为1-6随机. 测试两种方法的对比,用网上整理出的一张表如下 1.使用CriticalSection 方法时,有一个临界区cs 在将临界区传递给 InitializeCriticalSection 时(或者更准确地说,是

spinlock,mutex,semaphore,critical section的作用与区别

某年深信服的笔试题,考的就是多线程的同步,简单的解释下方便记忆: 1.spinlock:自旋锁.是专为防止多处理器并发而引入的一种锁. 2.mutex:互斥量.只有拥有互斥对象的线程才有访问公共资源的权限.保证了资源不会同时被多个线程访问. 3.semaphore:信号量.允许多个线程同时访问资源,限制访问资源的最大线程数. 4.critical section:临界区.任意时刻只允许一个线程对共享资源进行访问.

critical section的用法

critical section Critical Section: 不论是硬件临界资源,还是软件临界资源,多个进程必须互斥地对它进行访问.每个进程中访问临界资源的那段代码称为临界区(Critical Section). 每个进程中访问临界资源的那段程序称为临界区(Critical Section)(临界资源是一次仅允许一个进程使用的共享资源).每次只准许一个进程进入临界区,进入后不允许其他进程进入.不论是硬件临界资源,还是软件临界资源,多个进程必须互斥地对它进行访问. 多个进程中涉及到同一个临

spinlock,mutex,semaphore,critical section

critical section(临界区) 在任意时刻只允许一个线程对共享资源进行访问.如果有多个线程试图同时访问临界区,那么在有一个线程进入后其他所有试图访问此临界区的线程将被挂起,并一直持续到进入临界区的线程离开.临界区包含两个操作原语: EnterCriticalSection() 进入临界区 LeaveCriticalSection() 离开临界区. 优点:速度快.因为Critical Section不是内核对象,函数EnterCriticalSection()和LeaveCritica

General Critical Section Problem

Once I talked about Process Synchronization, and provided a demo program of Search/Insert/Delete Problem. As a matter of fact, such Critical Section Problems have a general form: given num different types of threads, as well as all pairs of i and j s

【JMeter_09】JMeter逻辑控制器__临界部分控制器&lt;Critical Section Controller&gt;

临界部分控制器<Critical Section Controller> 业务逻辑: 根据锁名来控制并发,同一个锁名之下,在同一时间点只能存在一个运行中,适用于控制并发的场景 锁名类型: 锁名为空,认为每个锁为不同的锁 锁名相同,多个锁认为是同一个锁,同一个时间点只能存在一个运行中 锁名为变量,根据变量值来判断是不是属于同一个锁,变量值为相同时,则认为是同一个锁 演示脚本: Demo_临界部分控制器 原文地址:https://www.cnblogs.com/fcholy/p/10902051.

链表,配合critical section

#include <windows.h> typedef struct _Node { struct _Node *next; int data; } Node; typedef struct _List { Node *head; CRITICAL_SECTION critical_sec; } List; List *CreateList() { List *pList = (List*)malloc(sizeof(List)); pList->head = NULL; Initia

[并发并行]_[线程同步]_[pthread和win32的临界区(Critical Section)比较]

场景: 1.  在多线程程序里,临界区是最常见的同步访问共享资源的最简单的解决方案. 2. pthread是跨平台的线程模型,那么它和本地的线程模型的临界区编程有什么区别呢? 临界区: 临界区是指一个小代码段,在代码能够执行前,它必须独占对某些资源的访问权.这是让若干代码能够"以原子操作方式"来使用资源的一种方法. 所谓原子(atomic)操作方式,是指这段代码知道没有别的线程要访问这个资源. 说明: 1.  MacOSX,Windows有自己的线程模型, pthread可以说是跨平台