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
701 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 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