?
Solved

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

Posted on 2002-03-08
7
Medium Priority
?
277 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 1200 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
Industry Leaders: 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 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

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

750 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