Solved

‘Amazingly Protecting’ a non-thread safe non multi-instance static library

Posted on 2002-03-08
7
243 Views
Last Modified: 2013-12-14
Hi,
I was supplied with a static library (lib which I do not have the source code for)
The library contains two methods:
A Set method & a Get method – the library is not multi-instance (I assume the variable being set or get is a global variable).

I then created a free-threaded com object (an ATL 7 attributed DLL containing a free threaded aggregated object).
I added 2 methods to the com object Set/Get each of these methods triggers the execution of the Set & Get methods of the static library (respectively).

I then wrote a tester which creates 2 instances of the com object.

Obj1,Obj1
It then performs Obj1->Set(1)
            Obj1->Set(2)
And runs an endless loop of
          Obj1->Get()
Obj2->Get()    

Both results of the Get methods are send to the output window using the ATLTRACE2 macro.

The amazing thing is – and finally here is my question – I get an endless output of 1 , 2 ,1 , 2 …
While what I expected would happen is an endless output of the number 1 OR the number 2 right after both of the Set methods where called.
(when I tried to code a similar library and use it the same way I did get the expected 1,2,1,1,1,1,1…)


Any ideas what is so special about this static library?
0
Comment
Question by:dr_moo
7 Comments
 
LVL 6

Accepted Solution

by:
snoegler earned 300 total points
ID: 6850201
I admit not having read your whole question, but i guess the answer is TLS.

If the lib uses global variables, they are put by default in a 'thread local storage'. This means, each thread uses its own instance of global variables from the point of view of the lib.

Cheers,
snoegler
0
 
LVL 86

Expert Comment

by:jkr
ID: 6850274
snoegler seems to be right - try declaring the global variables as '__declspec(thread)', e.g.

// Example of the thread attribute
__declspec( thread ) int tls_i = 1;
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6855144
I'm not an expert on this, but I can't see how TLS can be relevant -- aren't both objects created and excuted on the same thread?

dr_moo:
Use DUMPBIN to examine that .lib file.  There moight be some clue there.

Does the Lib file access a DLL?  If so, it might be able to track which instance of that DLL is being used and use that as an index into a local lookup table.  I really have no details on how this would work, but it is a thought.

Have you given us all of the details?  Does the Lib really have only two exports (waht about some sort of "Open" or "Init" fn that prepares for subsequent Get and Set calls?)?  And do the Get and Set functions require only a single scalar parameter (you illistrate with an int).  If the Get and Set actually pass a structure or a class, then that data could contain a cookie of some sort to easily keep track of stateless details.

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

 
LVL 6

Expert Comment

by:snoegler
ID: 6855511
A free threaded object is created in the same thread as the caller. I assumed that his example created the two objets in two seperate threads (to test the free-threaded functionality). The behavior he mentions happens often in this context, so i just guessed :)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6871900
hi dr_moo,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 11

Expert Comment

by:griessh
ID: 6955308
Dear dr_moo

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "snoegler"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 6979050
Force accepted

** Mindphaser - Community Support Moderator **
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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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

17 Experts available now in Live!

Get 1:1 Help Now