Solved

Thread safe?

Posted on 2001-07-31
9
547 Views
Last Modified: 2013-12-14
I have an ActiveX control that dynamically links to
another dll. This dll contains the following classes
which are defined in a cpp file (excuse the pseudo code):

class MyObject
{
   public:
      int m_refcount;
}

class MyObjectListMgr
{
   public:
      MyObject * getObject(int index);
   private:
      LIST m_myObjectList;
}

// Global static instance.
static MyObjectListMgr myObjectListMgr;

// In the same file another exposed class makes use of
// myObjectListMgr list this...

void AnotherClass::publicExposedMethod()
{
   MyObject * myObject = myObjectListMgr.getObject(0);

   myObject->m_refCount = myObject->m_refCount + 1;
}

My questions are:
   * If two of my controls are loaded by one application,
or by two different applications, then will they both be
using the exact same instance of myObjectListMgr?
   * Is this code thread-safe? If not, what are the
possible consequences?

I'm using Visual C++ 6.0 by the way.

Thanks,
Mike.
0
Comment
Question by:mnewton022700
9 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>My questions are:
>>  * If two of my controls are loaded by one application,
>>or by two different applications, then will they both be
>>using the exact same instance of myObjectListMgr?

No, the instantiation of global objects in a DLL is done on a per-process basis, so every process 'sees' it's own instance (note that ActiveX and DLL are interchangeable here). However, you could share a data area (not a class) in a shared data segment of the DLL - see http://support.microsoft.com/support/kb/articles/Q125/6/77.asp ('HOWTO: Share Data Between Different Mappings of a DLL')

>>  * Is this code thread-safe? If not, what are the
>>possible consequences?

That's not easy to say, as it depends on the ramifications of the code that's being called. The 'safest' (because pessimistic) answer is 'no'.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Another idea would be to use MapView functions to create a single buffer that can be shared among the multiple processes, threads, & Applications.
See:
CreateFileMapping
OpenFileMapping
MapViewOfFile
UnmapViewOfFile

You can create a MapView that doesn't use a file by calling CreateFileMapping() function with hFile of (HANDLE)0xFFFFFFFF
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
Comment Utility
>>  No, the instantiation of global objects in a DLL is done on a
>> per-process basis, so every process 'sees'
Jus to calrify this because that "no" is missleading.   If two different programs use the DLL, they do not share the global objkects and thus do not share the same list manager.   But if two controls are loaded by one program, they will share global objects and the same list manager.  Similarly two threads in seperate processes will not be accessing the same list manager or any other global object.  But two threads in the same process will be accessing the same list manager and other global objects.


>> Another idea would be to use MapView functions to create a
>> single buffer that can be shared among the multiple processes
Thsi is assuming that your goal is to try to make this extremely difficult to make thread safe.

However, if you want to make it thread safe without making it tought on yoruself, I would continue to not share memory between applications  However that alone is not enough to make it thread safe, but it menas yoiu can use critical sections to synchronize access instead of mutexes,   This will make your syncrhonizations much much faster.

I see that your code uses reference counting.  If reference counted objects can be shared between threads (and in general they can, you would have to take special precautions to be sure they don't.) then you must synchronize access to the reference count.  i.e. whenever an object's reference count is incremented or decremented, you need to do this from inside a critical section that protects the reference count.  Otherise the count could be corrupted and you will get memory leaks or you will get cases where an object is deleted 2 or more times.

I don't know what LIST is in

  LIST m_myObjectList;

but if it is some sort of linked list, and if this linked list will be used by more than one thread, then again you need to use it only from within a critical section.   If you fail to do this, two threads might alter the list at the same time and corrupt it, or one might search the llst while another modifies it and the search might terminate early or never end.   Bad stuff.
0
 
LVL 3

Author Comment

by:mnewton022700
Comment Utility
So not thread-safe then... I was pretty sure it wasn't, but I don't have a good handle on threads...

Thanks to jkr and Axter also.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
Multi-threading is often covered in genreal in Advanced programming text books.  

Lots of windows programming books will also have a chapter or two devioted to multi-threading too.  But be careful, some may just describe the features that windows provides for helping with multi-threading, but assume that you alrady have an understanding of the multi-threading issues--which is more important and far from obvious.  There is at least one book, Win32 Multi-threaded programming, by Cohen and Woodring, on the subject.


Sorry, jkr, once again not answering a question leads to such a fair result...
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>So not thread-safe then...

That's not for sure... I'd tend to say 'YES', but the snippet of code you provided isn't enought to thell that...
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I'd tend to say no.  I see no prevent that the referecne count and list will from being altered simmulataneously by 2 or more threads.  

Now there is no evidence that 2 or more threads could not do thiat at the same time.  So if this code is used by only one thread, then it is safe, but I suspect that is not true.  I suspect that 2 or more threads can be using this code and this will be unsafe as it is.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>I'd tend to say no

We seem to be a case of double negations here, as with "I'd tend to say 'YES'", I meant to agree to "not thread-safe" :o)

But, 'LIST' still could be a list type with serialized access...
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Actually its a partial double negation, since we're not sure... So its like -1 + +0.9, which leaves -0.1.  So...What does that mean?  :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 47
missing "main" in Java on Eclipse 4 56
debug as  junit test 4 64
Socket Programming (Unix) 8 98
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

762 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

9 Experts available now in Live!

Get 1:1 Help Now