Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++

Testing simple concurrent containers

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
25 Oct 2009CPOL10 min read 50.2K   404   40  
This article illustrates simple approaches and test results when creating containers with concurrent flavor and running on a multi-core PC.
/*
Copyright (c) 2009 Valery Grebnev.
CPOL (Code Project Open License) Licensed http://www.codeproject.com/info/cpol10.aspx
*/

#pragma once 

#include "stdafx.h"
#include "rwlocks_helpers.h"

template <typename key_type, typename value_type, typename lock_type>
class SRWL_map_t 
{
public:
	
	bool insert( const std::pair<key_type, value_type>& _val) 
	{
		internal_value value(_val.second);
		std::pair<key_type, internal_value> pair(_val.first, value);

		Exclusive_lock lock(m_global_scope_lock);
		return m_map.insert(pair).second;
	}

	bool erase(const key_type& _key)
	{
		Exclusive_lock lock(m_global_scope_lock);
		return (m_map.erase(_key) != 0);
	}

	bool update(const key_type& _key, const value_type& _val)
	{
		Shared_lock lock(m_global_scope_lock);

		map_iterator iterator = m_map.find(_key);
		if (iterator != m_map.end())
		{
			Element_exclusive_lock lock(iterator->second.m_lock);
			iterator->second.m_value = _val; 
			return true;
		}
		else
		{
			return false;
		}
	}

	bool find(const key_type& _key, value_type& _val)
	{
		Shared_lock lock(m_global_scope_lock);

		map_iterator iterator = m_map.find(_key);
		if (iterator != m_map.end())
		{
			Element_shared_lock lock(iterator->second.m_lock);
			_val = iterator->second.m_value; 
			return true;
		}
		else
		{
			return false;
		}
	}

private:	
	template <typename value_type>
	struct _internal_value
	{
		_internal_value(value_type value): m_lock(0), m_value(value) 
		{
		}
		volatile LONG CACHE_ALIGN m_lock;
		CACHE_PADDING1
		value_type m_value;
	};

	typedef _internal_value<value_type> internal_value;

	typedef ref_exclusive_lock_t<lock_type> Exclusive_lock;
	typedef ref_shared_lock_t<lock_type> Shared_lock;

	typedef exclusive_lock_t<ELEMENT_SPINLOCK, volatile LONG> Element_exclusive_lock;
	typedef shared_lock_t<ELEMENT_SPINLOCK, volatile LONG> Element_shared_lock;

#ifdef HAS_HASH_MAP
	typedef typename stdext::hash_map<key_type, internal_value>::iterator map_iterator;
	stdext::hash_map<key_type, internal_value> m_map;
#elif defined HAS_MAP
	typedef typename std::map<key_type, internal_value>::iterator map_iterator;
	std::map<key_type, internal_value> m_map;
#endif

	CACHE_PADDING1
	lock_type m_global_scope_lock;

};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions