Click here to Skip to main content
12,510,988 members (57,931 online)
Click here to Skip to main content

Tagged as

Stats

27.1K views
342 downloads
39 bookmarked
Posted

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.
/*
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

You may also be interested in...

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