Solved

Is CList thread-safe

Posted on 2003-11-26
10
806 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

12 Experts available now in Live!

Get 1:1 Help Now