Solved

Is CList thread-safe

Posted on 2003-11-26
10
822 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA "SendKeys" Syntax for Multiple Keystrokes 7 126
no14 challenge 14 73
isEverywhere  challenge 19 86
Message not shown 5 87
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…
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.

730 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