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

x
?
Solved

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

Posted on 2002-03-08
7
Medium Priority
?
268 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
[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
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
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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
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…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

636 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