Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Testing simple concurrent containers

, 25 Oct 2009
This article illustrates simple approaches and test results when creating containers with concurrent flavor and running on a multi-core PC.
container_locks.zip
Container_locks
src
Testing_simple_concurrent_containers.pdf
VC9
container_locks.vcproj.val-PC.val.user
Debug
tbb_debug.dll
Debug64
tbb_debug.dll
Release
tbb.dll
Release64
tbb.dll
/*
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)

Share

About the Author

Valery Grebnev
Software Developer
Canada Canada
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 25 Oct 2009
Article Copyright 2009 by Valery Grebnev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid