🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #10541

Re: Multithread calls one DLL function

#10541
From: Andrue Cope
Date: Fri, 02 Apr 2004 12:33
66 lines
3729 bytes
On Fri, 2 Apr 2004 12:58:24 +0200, Oliver Young wrote:

As a quick test if you don't understand why one of these functions is not
thread safe or how the first one is a thread safe (albeit pointless)
function then you need to do some research on threads:

// Note:These both assume that the standard libraries are thread safe.

// Safe function
int safeFn( char const * wibble )
{
  return strlen( wibble );
}

// Unsafe function
std::vector<char *> messagesRaisedByThisThread;
int unsafeFn( char const * wibble )
{
  messagesRaisedByThisThread.push_back( wibble );
}

Neither function should cause a crash but the latter is not likely to
behave the way you'd expect judging by the name of the vector.

> There will be no colision between DLLs/Functions in different threads? 
> I'm talking about fnMyFunc & MyAppDLL variables.

A thread is not a separate program it's just a separate execution point.
This means that conceptually if you have ten threads that all execute your
example code then it's exactly as if your code executed the example ten
times.

According to the normal Windows rules for DLLs this means that the calls to
LoadLibrary() and GetProcAddress() return /the same values every time/.
Whether you choose to give each thread its own variables or share one set
of globals doesn't really matter at first glance.

When thread (a) calls fnMyFunc() it sends the CPU to memory address
0x1234567 for its next instruction. When thread (b) calls fnMyFunc() it
sends to the CPU to /the exact same address/. This means that as given in
your example fnMyFunc() has no way of knowing which thread it is being
called by. This may or may not be a problem depending on what the function
does.

*In general you should assume that any function not explicitly documented
as thread safe is not thread safe*

*Functions that don't take arguments are particularly suspicious since
there is no way AFAIK that they can determine what thread called them*.

It is perfectly safe to have all the threads accessing the same copies of
MyAppDLL and fnMyFunc since they all have the same value.

Where it might become inadvisable is when each thread is responsible for
signing on and off the DLL. The problem then is how each thread is
maintaining the DLL load state and what if anything it does to the global
variables when it signs off the DLL. Your example is perfectly thread safe
even in this scenario as long as when you call FreeLibrary() you don't set
the variables back to NULL.

Oh, a further comment on my quick question. If you were to change the name
of the vector to 'messagesRaisedByAllThreads' then the function becomes
thread safe :)
-- 
Andrue Cope
[Bicester UK]
Technical Details
Message-ID: <198v7pvwk7zsm.c4wvaxcvd5hj$.dlg@40tude.net>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <406d3f8d@newsgroups.borland.com> <ygl4whupfvci$.164yempotbso8$.dlg@40tude.net> <406d474e$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,9fd9d633795f816c
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-04-02 04:39:38 PST
From: Andrue Cope <no.spam@spammy.com>
Subject: Re: Multithread calls one DLL function
Newsgroups: borland.public.cppbuilder.nativeapi
User-Agent: 40tude_Dialog/2.0.10.1
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
References: <406d3f8d@newsgroups.borland.com> <ygl4whupfvci$.164yempotbso8$.dlg@40tude.net> <406d474e$1@newsgroups.borland.com>
Date: Fri, 2 Apr 2004 13:33:39 +0100
Message-ID: <198v7pvwk7zsm.c4wvaxcvd5hj$.dlg@40tude.net>
NNTP-Posting-Host: 217.207.223.34
X-Trace: newsgroups.borland.com 1080909265 217.207.223.34 (2 Apr 2004 04:34:25 -0800)
Lines: 66
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail