thread safety and std::map

Discussion in 'C++' started by digz, Mar 4, 2007.

  1. digz

    digz Guest

    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_t> timerMap;

    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();
    }

    ----------------
     
    digz, Mar 4, 2007
    #1
    1. Advertising

  2. digz

    Jim Langston Guest

    "digz" <> wrote in message
    news:...
    > 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_t> timerMap;
    >
    > 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... ?
     
    Jim Langston, Mar 4, 2007
    #2
    1. Advertising

  3. digz

    digz Guest

    On Mar 4, 1:43 pm, "Jim Langston" <> wrote:
    > "digz" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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_t> timerMap;

    >
    > > 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..
     
    digz, Mar 6, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,423
    Ivan Vecerina
    Mar 17, 2005
  2. neilsolent

    std::map iteration safety

    neilsolent, Dec 16, 2008, in forum: C++
    Replies:
    3
    Views:
    824
    Kai-Uwe Bux
    Dec 17, 2008
  3. Replies:
    1
    Views:
    451
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,046
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    2,064
    James Kanze
    Dec 21, 2008
Loading...

Share This Page