|
|
|
|
|
|
|
|
- 프로그래밍 : 06/08/11 15:13 |
|
|
|
뮤텍스와 세마포어의 경우 콘솔 레벨에서 동기화를 하므로,
보다 포괄적이지만 그만큼 연산이 느리다는 단점이 있다.
따라서 프로세스 레벨에서 동기화 시키는 크리티컬 섹션이
보다 연산이 빠르기 때문에 게임에는 주로 이용된다.
뭔가 있어 보이지만 사용은 무척 쉽다;
**뮤텍스
void MutexExample()
{
static HANDLE _mutex;
// Mutex생성
if( _mutex== NULL )
{
// 초기화
_mutex= CreateMutex(NULL, FALSE, "test");
}
// 락
WaitForSingleObject( _mutex, INFINITE );
// 코드 //
// 언락
ReleaseMutex(_mutex);
}
**크리티컬 섹션
// 함수 선언
class CriticalSectionExample
{
void Init();
void Uninit();
void Update();
// 멤버 변수
CRITICAL_SECTION _critsec;
}
void CriticalSectionExample::Init()
{
// 초기화 함수
InitializeCriticalSection( &_critsec );
}
void CriticalSectionExample::Uninit()
{
// 종료 함수
DeleteCriticalSection( &_critsec );
}
void CriticalSectionExample::Update()
{
// 락
EnterCriticalSection( &_critsec );
// 코드 //
//언락
LeaveCriticalSection( &_critsec );
} |
|
|
|
관련글(5757) :
댓글(699)
|
|
|
|
- 프로그래밍 : 06/02/14 10:47 |
|
|
|
o STL history
- Alexander Spepanov, late 70s, <<>>
- 1985, generic Ada library
- 1987, C++ was premature
- 1988~1992, HP algorithm project
- 1994, STL was adopted into the draft standard ANSI/ISO C++
o C++ for STL
- Nice Classes( HP & Andrew Koenig from the Bell Labs)
. Copy constructor : T (const T&)
. Assignment operator : T& operator= (const T&)
. Equality operator : int operator== (const T&, const T&)
. Inequality operator : int operator!= (const T&, const T&)
- Function objects : operator()
- Function Templates
template
void swap (T& a, T& b)
T tmp = a;
a = b;
b = tmp;
}
- Class Templates
template
class vector {
T* v;
int sz;
public :
vector (int s) { v= new T[sz=s]; }
~vector () { delete [] v; }
T& operator[] (int i) { return v[i]; }
int get_size() { return sz; }
};
o A STL overview
- STL is a component library.
. Algorithm : computational procedure that is able to work on different containers
. Container : object that is able to keep and administer objects
. Iterator : abstraction of the algorithm-access to containers so that an algorithm is able to work on different containers
. Function Object : a class that has the function-call operator (operator() ) defined
. Adaptor : encapsulates a component to provide another interface (e.g. make a stack out of a list)
o Containers
- Sequence Containers
. Vector
. Deque
. List
- Associative Containers
. Set
. Multiset
. Map
. Multimap
o Iterators - a generalization of pointers
- operator*
- operator++
- Sample Iterator
vector v(3);
v[0] = 5;
v[1] = 2;
v[2] = 7;
vector::iterator first = v.begin();
vector::iterator last = v.end();
while (first != last)
cout << *first++ << " ";
- sort(v.begin(), v.end() );
- Input Iterators and Output Iterators for single pass algorithms
- Forward Iterators for multi-pass one-directional algorithms
- Bidirectional Iterators for multi-pass algorithms
- Random Access Iterators for random access algorithms
o Algorithms
- algorithm example : binary search
- Step 0 : Initial Step
const int* binary_search (const int* array, int n, int x) {
const int* lo = array, *hi = array + n, *mid;
while(lo != hi) {
mid = lo + (hi - lo) / 2;
if (x == *mid) return mid;
if (x < *mid) hi = mid; else lo = mid + 1;
}
return 0;
}
- Step 1 : Templates
template
const T* binary_search (const T* array, int n, const T& x) {
const T* lo = array, *hi = array + n, *mid;
while(lo != hi) {
mid = lo + (hi - lo) / 2;
if (x == *mid) return mid;
if (x < *mid) hi = mid; else lo = mid + 1;
}
return 0;
}
- Step 2 : Null Pointer
template
const T* binary_search (const T* array, int n, const T& x) {
const T* lo = array, *hi = array + n, *mid;
while(lo != hi) {
mid = lo + (hi - lo) / 2;
if (x == *mid) return mid;
if (x < *mid) hi = mid; else lo = mid + 1;
}
return array + n;
}
- Step 3 : Iterator
template
const T* binary_search (T* first, T* last, const T& value) {
const T* lo = array, *hi = array + n, *mid;
while(lo != hi) {
mid = lo + (hi - lo) / 2;
if (value == *mid) return mid;
if (value < *mid) hi = mid; else lo = mid + 1;
}
return last;
}
- Step 4 : No more pointers but iterators
template
RandomAccessIterator binary_search (RandomAccessIterator first,
RandomAccessIterator last,
const T& value) {
RandomAccessIterator not_found = last, mid;
while(first != last) {
mid = first + (last - first) / 2;
if (value == *mid) return mid;
if (value < *mid) last = mid; else first = mid + 1;
}
return not_found;
}
o Fuction Objects
- unary_function
template
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
};
- unary function object example
template
struct negate : unary_function {
T operator()(const T& x) const { return -x; }
};
- binary_function
template
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
- binary function object example
template
struct logical_and : binary_function {
bool operator()(const T& x, const T& y) const { return x && y; }
};
o Adaptors - template classes that provides interface mappings
- Container Adaptors
. A stack from a vector, a list or a deque.
. A queue from a list or a deque.
. A priority queue from a vector or a deque.
- Iterator Adaptors
. Reverse Iterators
. Insert Iterators
. Raw Storage Iterator
- Function Adaptors
. Negators
. Binders
. Adaptors for pointers to functions
o Allocators and memory handling
- Objects that encapsulate following information
· pointer types
· type of their difference (difference type ptrdiff_t)
· type of the size of objects in a memory model (size type size_t)
· memory allocation and deallocation primitives. |
|
|
|
관련글(7363) :
댓글(706)
|
|
|
|
|
|
|
|
|