Solved

How safe is using a common CMapStringToPtr declared globally in a thread which is called simulataniously more than 500 times?

Posted on 2007-11-17
16
727 Views
Last Modified: 2013-11-20
How safe is using a common CMapStringToPtr declared globally in a thread which is called simulataniously more than 500 times?

I have declared CMapStringToPtr as global variable and using it inside a thread.
The thread is called simulataniously many times
Should I use any sync objects to we can leave that as it is?

Thanks
Eugene
0
Comment
Question by:shiffin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
16 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20306237
how many times is your map written? just once? or many times?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20306263
CMap objects are not thread-safe, according to MS documentation. So, multithreading writing is absolutely risky.
In theory, multithreading reading should work, since internal MFC implementation doesn't seem to have static members, but I wouldn't take the risk in a critical application. So, better you use a CMutex object to protect access.
0
 

Author Comment

by:shiffin
ID: 20306270
Hi
Many times. Since thread, there is a possibility of "SetAt" called same time (same object).
I have no practical issues when I do that even on 100 threads, but, I want to be very sure on this.
Thanks
Eugene
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20306278
No, sometime you will have a simultaneous write and your collection will corrupt. I have experienced that many times.
0
 

Author Comment

by:shiffin
ID: 20306284
What is the solution?
Regards
Eugene
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20306395
Derive your own Map:

class CMyMap : protected CMapStringToPtr
{
protected:
        CMutex m_Mutex;

public:
        void SetAt(LPCTSTR key, void *value)
        {
                 CSingleLock lock(&m_Mutex);
                 lock.Lock();
                 CMapStringToPtr::SetAt(key, value);
                 lock.Unlock();
        }
        void *GetAt(LPCTSTR key)
        {
                 void *ret;

                 CSingleLock lock(&m_Mutex);
                 lock.Lock();
                 ret = CMapStringToPtr::GetAt(key);
                 lock.Unlock();

                return ret;
        }
}
0
 

Author Comment

by:shiffin
ID: 20306464
Seems to be more professional. My application is similer to IIS (In Async) running more than 1000 connections and many critical operations in thread.
Will this make any performance issue? Because, all other threads have to wait till one lock process is busy.
Thanks
Eugene
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20306506
depends on service load, indeed it will be a bit slower than not using mutex, but it will be slower where collisions are produced. Just look at your performance window.
0
 

Author Comment

by:shiffin
ID: 20306530
I am not beginer, but, I have not worked on the "performance window", which I belive must one of the very useful area.
Could you please guide me on how and where do I check the "performance window"?
Thanks
Eugene
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20306547
just press ctrl-alt-del to see the Task Manager, press the "Performance" tab.
0
 

Author Comment

by:shiffin
ID: 20306567
I normally look at only "Processes" and the CPU time here.
Now looking at the "Performance" tab. Could you please tell me the approximate ideal values here?
CPU Usage and PF Usage.
When do we get alert?
What are the worst condition?
Thanks
Eugene
0
 

Author Comment

by:shiffin
ID: 20306594
In your previous sample, you have the line...

ret = CMapStringToPtr::GetAt(key);

I think it should be Lookup with two parameters.

Could you please rewrite the codings?
Thanks
Eugene
0
 

Author Comment

by:shiffin
ID: 20306602
I did as below
Is it ok?
Eugene

    BOOL Lookup(LPCTSTR key,void*& rValue)
    {
		
		CSingleLock lock(&m_Mutex);
		lock.Lock();
		BOOL ret = CMapStringToPtr::Lookup(key,rValue);
		lock.Unlock();
		return ret;
    }

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20306605
you can use as:

       void *GetAt(LPCTSTR key)
        {
                 void *ret;

                 CSingleLock lock(&m_Mutex);
                 lock.Lock();
                 if (!Lookup(key, ret))
                      ret = NULL;
                 lock.Unlock();

                return ret;
        }

if there is not key present, it will return NULL
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20306607
both will work, I like mine more ;)
0
 

Author Comment

by:shiffin
ID: 20306630
After looking at your code, even I like yours.
Thanks for the help. Its all clear.
Eugene
0

Featured Post

Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question