• C

Multiprocessor NT gives me problem!

Problem summary:

A lib file was delivered to me as client API towards a socketbased application.

I had to write a dll in C that exported the functions in the LIB file so that I could call them from my VB code.
I could then reach the API from VB using the declare statement.

My VB code is built in VB6.0 as a COM component (dll) using Appartment threading and is running under MTS.

The C dll is built in VC6.0 and compiled as Multithreaded.
The c code just passes the parameters further to the lib function.

Now when I put my code under some preassure Errors occures... on the multiprocessor system the Error is much more frequent.

I put my C dll under preasure, and no error occures... When the calls pass through the VB dll the error comes !

Could this have something to do with trhreads and threadsafe applications?
How can I verify that my c dll is "threadsafe" ?

And what can solve my problem ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>I put my C dll under preasure, and no
>error occures... When the calls pass
>through the VB dll the error comes !

>Could this have something to do with
>trhreads and threadsafe applications?
Yes, it sure could.  Merely compiling with the multithreaded option is NO GUARANTEE that your app or dll will be multithreaded safe.  It just chooses the multithreaded runtime library that supports the capability of writing a thread-safe application.
>How can I verify that my c dll
>is "threadsafe" ?

Well, it depends on what you mean by "verify".  You've successfully verified that it is NOT threadsafe by your testing.  Fix the problem and then do the same kinds of testing to verify that it is.  

First, however, you should review all the code that runs in a separate thread and ensure that all shared objects are protected.
JediAuthor Commented:
I have no code that is written to handle threads. My c dll just declares the functions in the library, accepts parameters calls the library. It also exports the functions using pascal calling convention so that VB can use them.

The C dll can be called and no error occures. What makes my calling chain "unthreadsafe" then.
Is it the c dll ?, I've tested it under preasure and it doesnt crash.

Is it the VB COM dll, how can it be built to handle threads ?

Is it something in the MTS ?

Some advice?
On how to test, what to test
You've really posted so little information about the structure of your application that it's difficult to do anything but speculate.
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

JediAuthor Commented:
Please tell me what more structure information you need about the application!

Does anyone know of any way to verify that a dll built in MSVC 6.0 using the multithread option is threadsafe ?


Structure of the application:
A VB 6.0  built Active X dll, built with the appartment threading model is calling functions in a C dll.

The C dll is called using pascal calling convention which is the way of calling functions in a dll from VB. When there's no COM based interface.

The C dll uses a static library with functions. This library is linked into the dll.The C dll is built in MSVC 6.0 using the multithread option.


The VB built dll chrashes constantly in a multiprocessor environment, and seldom in a single processor environment.

>I have no code that is written to
>handle threads.

Hmmmm, it seems like you've answered your own question!!!  You've chosen to use a technology (i.e. ActiveX w/Apartment threading) that causes multiple theads to be used.  Yet you have not written any code in the client DLL (i.e. your C++ dll) that handles multiple threads.  As I've already said, merely linking with the multithreaded runtimes does not ensure that your code is thread safe.

You've already identified that the source of the problem is your C++ DLL.  Now you need to analyze your code and find those places where you've made poor decisions in the design of the code.  If you are looking for some kind of magic compiler switch to solve your problem, give it up.  Multi-threaded programming is non-trivial and there are no shortcuts.  Proper code analysis and practice are the only solutions.
JediAuthor Commented:
Ok thanks for your comments sofare...

This discussion is helping me understand the problem.
My thaughts...now

The ActiveX dll:s are loaded into several simoultanious appartments. The C dll is through the VB declare statement loaded to memory in only one instance. This requires that the c dll can handle theese calls simoultaniously(fork or que)

Since my C dll is the entrance to the functions in the API for the calling processes(there can be many simultanious processes since the calling processes uses the appartment threading model)
it must in code handle the multithread environment by
? creating a new thread for each calling process to execute in.
? Use semafores or critical sections from the OS (WINNT) to queue all requests.

The  code in the C:dll is only declarations of functions and body of the functions passes parameters to the static library.

Could  I solve this with the methods I've listed:
Use a semafore, or critical section for
all my functions.
This will mean that I'm queing the requests of all processes, but it will be safe.
From your comments, I gather than you don't have control over the code that is in the static library, correct.  

If so then your idea to have your DLL serve as a "gatekeeper" to the library could work.  If we assume that the problem is include the static library functions (due to their being non-threadsafe) then one way to solve the problem without re-writing the static library, would be to make sure that it's functions are called in such a way as to prevent simultaneous multithreaded access.

On potential problem to be careful of with a system like this, however, is deadlock.  If there is any interaction between the threads in your application you could very easily get into the situation where thread A has the static library locked for it's exclusive use but is waiting for thread B.  Thread B, however, is waiting to get access to the static library.  Deadlock!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JediAuthor Commented:
I don't have any control of, or code to the static library.

How do I know whether it is the static library  that contains the code that is
non-threadsafe ?
If I could locate the non-threadsafe code i would be very happy.

My code in the C dll as I said only declares functions and accepts parameters and passes them on to the static library.
And in VB i dont know if you can handle threads.
JediAuthor Commented:
My working day is almost over...see you tomorrow

JediAuthor Commented:
Thanks for the help anyway....
I think we're on to the problem now. Just needed to stop and think of what we're building for a moment...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.