synchronized.cpp

来自「good luck to everyone!」· C++ 代码 · 共 32 行

CPP
32
字号
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved.							  */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib.  */
/*----------------------------------------------------------------------------*/

#include "Synchronized.h"

/**
 * Synchronized class deals with critical regions.
 * Declare a Synchronized object at the beginning of a block. That will take the semaphore.
 * When the code exits from the block it will call the destructor which will give the semaphore.
 * This ensures that no matter how the block is exited, the semaphore will always be released.
 * Use the CRITICAL_REGION(SEM_ID) and END_REGION macros to make the code look cleaner (see header file)
 * @param semaphore The semaphore controlling this critical region.
 */
Synchronized::Synchronized(SEM_ID semaphore)
{
	m_semaphore = semaphore;
	semTake(m_semaphore, WAIT_FOREVER);
}

/**
 * Syncronized destructor.
 * This destructor frees the semaphore ensuring that the resource is freed for the block
 * containing the Synchronized object.
 */
Synchronized::~Synchronized()
{
	semGive(m_semaphore);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?