Solved

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

Posted on 2002-03-08
7
252 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
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.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Software tool to depict the structure of a Visual Studio solution/project 6 102
thread-safe code in c++ 2 115
Arduino EDI - Programming language 3 103
How do i run a c++ file? 15 46
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
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…

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