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

Testing simple concurrent containers

, 25 Oct 2009 CPOL
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, WORD number_of_threads>
class 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);

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

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

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

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

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

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

private:	
	typedef Container_locker_t<lock_type, number_of_threads> Container_locker;
	typedef typename Container_locker::internal_value<value_type> internal_value;
	
#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
	Container_locker m_map_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 | Terms of Use | Mobile
Web03 | 2.8.150302.1 | Last Updated 25 Oct 2009
Article Copyright 2009 by Valery Grebnev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid