Solved

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

Posted on 2002-03-08
7
248 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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