Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
739 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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 2000 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 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