Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Is CList thread-safe

Posted on 2003-11-26
10
Medium Priority
?
843 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Assisted Solution

by:migel
migel earned 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Accepted Solution

by:
AlexFM earned 800 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

610 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