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
686 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to gracefully close the c++ 11 thread? 3 83
Which Linux flavors will this run on? 6 76
gdb doesn't stop on breakpoint 2 50
What is sub-make ? 2 39
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

896 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now