Solved

Is CList thread-safe

Posted on 2003-11-26
10
811 Views
Last Modified: 2013-11-20
I have one global CList<> object in app and many threads. Is syncronization built-in into CList?
If not, what will be the easiest way to sync access to global CList object?
0
Comment
Question by:amakalski
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Assisted Solution

by:migel
migel earned 50 total points
ID: 9827447
Hi!
All collections in MFC isn`t thread safe:
IMHO The best way to get thread safety list is write thread safe wrapper around list. Or get thread safe collections from boost library.
0
 

Author Comment

by:amakalski
ID: 9828823
Are std collections thread-safe?

>Or get thread safe collections from boost library
Boost library? Never heard of that, where can I read about it?

I will add points for someone who can show how to code safe wrapper?
0
 
LVL 12

Expert Comment

by:migel
ID: 9830108
For what MFC version you need this class?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 9830115
Suppose you need to write safe wrapper for CList<> class. I recommend to use STL and not MFC container, but it doesn't matter. Safe-thread class has critical section which protects all it's functions from parallel execution in various threads:

class CThreadSafe
{
public:
    <any functions accessing the list>

protected:
    CList<...> m_list;
    CCriticalSection m_CriticalSection;
};

For example, function which adds new element to list:

void CThreadSafe::Add(int n)
{
    m_CriticalSection.Lock();

    // all code between Lock and Unlock is thread-safe
    m_list.AddTail(n);

    m_CriticalSection.Unlock();
}

or, using CSingleLock class, which unlocks critical section in destructor:

void CThreadSafe::Add(int n)
{
    CSingleLock lock(&m_CriticalSection, TRUE);

    // all code between after this line is thread-safe

    m_list.AddTail(n);
}

0
 
LVL 12

Expert Comment

by:migel
ID: 9830122
oops :-)
A can`t write so much code so fast :-)
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.

 
LVL 4

Assisted Solution

by:inox
inox earned 50 total points
ID: 9831040

that what commented before may be right, but also may not help you. In a way you use that list you must protect outside the List, so a 'protected list' is not suitable and can't be done in a wrapper class:

imagin following situation:

thread 1 calls something like:
1   pos=Lst.GetHeadposition()
2   ptr=Lst.Getnext(pos)
3   ptr->dosomething()

thread 2 calls something like:
1   pos=Lst.GetHeadposition()
2   ptr=Lst.Getnext(pos)
3   ..
4  delete ptr
5  ...

if thread 2 interrupts thread 1 between line1 and 2 ......  :-(
situations like that can hardly be protected by a wrapper class
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9831158
I don't mean that every CList function should be wrapped in the separate wrapper function. Every function which user needs should be wrapped, and it may contain number of calls to internal CList. For example:

void CThreadSafe::SomeComplicatedFunction(...)
{
   CSingleLock lock(&m_CriticalSection, TRUE);

   // thread-safe code
   pos=m_list.GetHeadposition();
   ptr=m_list.Getnext(pos);
   ptr->dosomething();
   ...
}

Thread-safe class may expose also functions Lock and Unlock which allow to client to call number of function by thread-safe way:

pCThreadSafe->Lock();    // internally locks m_CriticalSection

pCThreadSafe->Function1();
pCThreadSafe->Function2();
...

pCThreadSafe->Unlock();    // internally unlocks m_CriticalSection
0
 
LVL 4

Expert Comment

by:inox
ID: 9831654

AlexFM,
I agree on your comments. I only wanted to point out to the difference of List-operations-threadsafety and List-contents-threadsafety (even thread-relating problems not outlined completely)

Also I mentioned "you must protect outside the List", thats what you confirmed in your 2nd comment with "pCThreadSafe->Lock();" (outside means: not inside a method of the wrapperclass)

I wanted to prevent amakalski beeing in the assumption he could freely use any operations if he does protect just (and only) inside a  wrapper class.
An exception of this is if it is a wrapper that hides the origin List completely behind a locked list (like Delphi)
0
 

Author Comment

by:amakalski
ID: 9832512
Thanks everybody,
especially AlexFM for excellent code.
0
 

Author Comment

by:amakalski
ID: 9832539
And for future readers of this solution (as inox and AlexFM noted). If you need to iterate through the list outside of the CThreadSafe class
THEN use something like

threadsafelist->Lock();
// Iteration code goes here

threadsafelist->Unlock();
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 create frequencies of a variable from SAS dataset? 10 121
Need Help INsttalling wget on Mavericks OS X 3 90
bigDiff challenge 17 84
Fibonacci challenge 11 110
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

14 Experts available now in Live!

Get 1:1 Help Now