473,756 Members | 5,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

thread safety and std::map

Hi,
I am trying to write a program which has two threads one of them write
to a map , and the other one deletes entries based on a certain
criterion..
first I cannot get the delete portion to work , what am i missing
here.
also is it possible/correct that the removeKeyValue function acquire
the mutex lock only during the call to map.erase(), and not the during
the whole iteration process as i have done here( i logically felt it
was safer to lock up the whole iteration ) but that can take up
massive amounts of time when the addKeyValue function would wait for
the lock to be released...

Thanks in advance
Digz
#include<map>
#include<iostre am>
#include<boost/thread/thread.hpp>
#include<boost/thread/mutex.hpp>
#include<boost/bind.hpp>
#include<time.h >

boost::mutex _mutex;
typedef boost::mutex::s coped_lock Lock;
typedef std::map <int, time_t>::iterat or mapIter;
std::map <int, time_ttimerMap;

void addKeyValue (std::map < int, time_t &map) {
int i = 0;
time_t t;
while (true) {
{
Lock guard (_mutex);
map[++i] = time (&t);
std::cout << "added: " << i << ", " << t << std::endl;
} //end lock
sleep (1);
} //end while
}

void removeKeyValue (std::map < int, time_t &map) {
time_t t;
while (true) {
{
Lock guard1 (_mutex);
mapIter end = map.end ();
for (mapIter beg = map.begin (); beg != end;) {
if (time (&t) - (*beg).second 3) {//anything older than 3
seconds needs attention
std::cout << "removing: " << (*beg).first << ", " <<
(*beg).second << std::endl;
map.erase (beg++);
}
else
++beg;
} //end for
} //end lock scope
} //end while true
}

int main ()
{
boost::thread t (boost::bind (addKeyValue, timerMap));
boost::thread u (boost::bind (removeKeyValue , timerMap));
t.join();
u.join();
}

----------------

Mar 4 '07 #1
2 5377
"digz" <Di********@gma il.comwrote in message
news:11******** **************@ j27g2000cwj.goo glegroups.com.. .
Hi,
I am trying to write a program which has two threads one of them write
to a map , and the other one deletes entries based on a certain
criterion..
first I cannot get the delete portion to work , what am i missing
here.
also is it possible/correct that the removeKeyValue function acquire
the mutex lock only during the call to map.erase(), and not the during
the whole iteration process as i have done here( i logically felt it
was safer to lock up the whole iteration ) but that can take up
massive amounts of time when the addKeyValue function would wait for
the lock to be released...

Thanks in advance
Digz
#include<map>
#include<iostre am>
#include<boost/thread/thread.hpp>
#include<boost/thread/mutex.hpp>
#include<boost/bind.hpp>
#include<time.h >

boost::mutex _mutex;
typedef boost::mutex::s coped_lock Lock;
typedef std::map <int, time_t>::iterat or mapIter;
std::map <int, time_ttimerMap;

void addKeyValue (std::map < int, time_t &map) {
int i = 0;
time_t t;
while (true) {
{
Lock guard (_mutex);
map[++i] = time (&t);
std::cout << "added: " << i << ", " << t << std::endl;
} //end lock
sleep (1);
} //end while
}

void removeKeyValue (std::map < int, time_t &map) {
time_t t;
while (true) {
{
Lock guard1 (_mutex);
mapIter end = map.end ();
for (mapIter beg = map.begin (); beg != end;) {
if (time (&t) - (*beg).second 3) {//anything older than 3
seconds needs attention
std::cout << "removing: " << (*beg).first << ", " <<
(*beg).second << std::endl;
map.erase (beg++);
}
else
++beg;
} //end for
} //end lock scope
} //end while true
}

int main ()
{
boost::thread t (boost::bind (addKeyValue, timerMap));
boost::thread u (boost::bind (removeKeyValue , timerMap));
t.join();
u.join();
}

----------------
comp.programmin g.threads may be a better newsgroup for this question.

You say, "I cannot get the delete portion to work" how is it not working?
Is it not deleting the entries or locking up or... ?

Mar 4 '07 #2
On Mar 4, 1:43 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"digz" <Digvijo...@gma il.comwrote in message

news:11******** **************@ j27g2000cwj.goo glegroups.com.. .
Hi,
I am trying to write a program which has two threads one of them write
to a map , and the other one deletes entries based on a certain
criterion..
first I cannot get the delete portion to work , what am i missing
here.
also is it possible/correct that the removeKeyValue function acquire
the mutex lock only during the call to map.erase(), and not the during
the whole iteration process as i have done here( i logically felt it
was safer to lock up the whole iteration ) but that can take up
massive amounts of time when the addKeyValue function would wait for
the lock to be released...
Thanks in advance
Digz
#include<map>
#include<iostre am>
#include<boost/thread/thread.hpp>
#include<boost/thread/mutex.hpp>
#include<boost/bind.hpp>
#include<time.h >
boost::mutex _mutex;
typedef boost::mutex::s coped_lock Lock;
typedef std::map <int, time_t>::iterat or mapIter;
std::map <int, time_ttimerMap;
void addKeyValue (std::map < int, time_t &map) {
int i = 0;
time_t t;
while (true) {
{
Lock guard (_mutex);
map[++i] = time (&t);
std::cout << "added: " << i << ", " << t << std::endl;
} //end lock
sleep (1);
} //end while
}
void removeKeyValue (std::map < int, time_t &map) {
time_t t;
while (true) {
{
Lock guard1 (_mutex);
mapIter end = map.end ();
for (mapIter beg = map.begin (); beg != end;) {
if (time (&t) - (*beg).second 3) {//anything older than 3
seconds needs attention
std::cout << "removing: " << (*beg).first << ", " <<
(*beg).second << std::endl;
map.erase (beg++);
}
else
++beg;
} //end for
} //end lock scope
} //end while true
}
int main ()
{
boost::thread t (boost::bind (addKeyValue, timerMap));
boost::thread u (boost::bind (removeKeyValue , timerMap));
t.join();
u.join();
}
----------------

comp.programmin g.threads may be a better newsgroup for this question.

You say, "I cannot get the delete portion to work" how is it not working?
Is it not deleting the entries or locking up or... ?
Thanks
Will post to the correct list ,however to be clear
It is not deleting the entries , when i try gdb debug , the begin !=
end test always fails
and the deleting thread is never able to enter the iteration loop,
In normal execution , i only see stuff getting added (the cout
messages)
but none of getting deleted..

Mar 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
30083
by: Woodster | last post by:
I have declared the following std::map<std::string, std::string> myMap; to pass myMap to functions should I be declaring functions as: void function(std::map<std::string, std::string>); or is there a preferred/better method of doing this?
1
1867
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
2
3405
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string, MathFuncPtr> predefinedFunctions; // lots of other stuff void makeDictionary(){ predefinedFunctions=&F::f_sin(); } };
1
3553
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my program uses two MFC classes: CRect and CPoint which represents Rectangle and Point concepts (as usual) and a user
19
6163
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
3
3714
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I can't think of a good fix, or even why it broke. The problem In one of my CFormView derived classes I have a member variable of the type...
1
6479
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed under Visual Studio .NET 2003 in a Win32 Console Project // VectorInsert.cpp : Defines the entry point for the console application / #include "stdafx.h #include "VectorInsert.h #ifdef _DEBU
13
9674
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
8
4074
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential search in a vector. Has anyone run into this before?
0
9455
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.