473,224 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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<iostream>
#include<boost/thread/thread.hpp>
#include<boost/thread/mutex.hpp>
#include<boost/bind.hpp>
#include<time.h>

boost::mutex _mutex;
typedef boost::mutex::scoped_lock Lock;
typedef std::map <int, time_t>::iterator 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 5336
"digz" <Di********@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.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<iostream>
#include<boost/thread/thread.hpp>
#include<boost/thread/mutex.hpp>
#include<boost/bind.hpp>
#include<time.h>

boost::mutex _mutex;
typedef boost::mutex::scoped_lock Lock;
typedef std::map <int, time_t>::iterator 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.programming.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...@rocketmail.comwrote:
"digz" <Digvijo...@gmail.comwrote in message

news:11**********************@j27g2000cwj.googlegr oups.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<iostream>
#include<boost/thread/thread.hpp>
#include<boost/thread/mutex.hpp>
#include<boost/bind.hpp>
#include<time.h>
boost::mutex _mutex;
typedef boost::mutex::scoped_lock Lock;
typedef std::map <int, time_t>::iterator 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.programming.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
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>); ...
1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
2
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,...
1
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...
19
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...
3
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...
1
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...
13
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
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.