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
678 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

19 Experts available now in Live!

Get 1:1 Help Now