HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

C++ critical section with timeout

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withtimeoutcriticalsection

Problem

NOTE: My implementation is based on codeproject article by Vladislav Gelfer.

Based on valdok's codes, I rewrote the critical section class. The difference is that my implementation integrated recursive(reentrantable) locking feature into a single critical section class. (FYI, valdok provides 2 classes: one without recursion and the other with recursion.)

I just want to verify that my implementation is still correct and intact.

Please, do not try to argue that the implementation is meaningless against using the WIN32 critical section object. I think this implementation has many advantages for many reasons. And, one reason is I needed to provide try_enter() feature for all versions of Windows.

```
#pragma once

#include
#include
#pragma intrinsic(_WriteBarrier)
#pragma intrinsic(_ReadWriteBarrier)

class critical_section
{
private:
struct cpu_type
{
enum type
{
unknown,
single,
multiple
};
};

public:
critical_section(u32 spin_count=0)
: m_semaphore(null)
, m_thread_id(0)
, m_wait_count(0)
, m_spin_count(0)
, m_recur_count(0)
{
// determine the cpu type
if( m_cpu_type == cpu_type::unknown )
{
SYSTEM_INFO sys_info;
::GetSystemInfo(&sys_info);

m_cpu_type = (sys_info.dwNumberOfProcessors > 1)?cpu_type::multiple:cpu_type::single;
}

// set the spin count.
set_spin_count(spin_count);
}

~critical_section()
{
if(m_semaphore != null)
{
CloseHandle(m_semaphore);
m_semaphore = null;
}

::memset(this, 0, sizeof(*this));
}

void set_spin_count(u32 count)
{
// on single core, there should be no spinning at all.
if(m_cpu_type == cpu_type::multiple)
{
m_spin_count = count;
}
}

public:
bool enter(u32 timeout=INFINITE)
{
u3

Solution

To be honest it is quite difficult to verify this sort of thing works by dry running code.

I would take the original code and design some test cases which show it working.
Run them with and without the code.

Then design some test cases which fail due to the problem you have spotted, or perhaps do not work well enough without your enhancement.

Run those test cases over your new code (all of them not just the newest ones) and hey presto you know if your code works.

Context

StackExchange Code Review Q#1836, answer score: 5

Revisions (0)

No revisions yet.