Loadlibrary(), function callbacks and multi-threading

B

Bit byte

I am writing an application that requires me to load a (Win32 C) libray
and then register several call back functions.

I am loading the libary in a worker thread and then registering
functions in the worker thread in the loaded library.

Two questions

1). Does loadlibrary(0 run the executable in the same thread as the
calee (probably not)
2). Is it safe to register/call callback funcs accross threads (assuming
functions are written so as note to alter state of calee)
 
C

Carl Daniel [VC++ MVP]

Bit said:
I am writing an application that requires me to load a (Win32 C)
libray and then register several call back functions.

I am loading the libary in a worker thread and then registering
functions in the worker thread in the loaded library.

Two questions

1). Does loadlibrary(0 run the executable in the same thread as the
calee (probably not)

LoadLibrary doesn't "run the executable" when loading a DLL, except for the
DllMain function. That IS run in the context of the thread that called
LoadLibrary. Subsequent calls to fuinctions in the DLL will occur in
whatever thread(s) make the call(s) - there's no hidden thread switch back
to the thread that loaded the DLL or anything like that (in contrast with,
for example, calling into a STA-threaded COM object, where such a thread
switch does indeed occur).
2). Is it safe to register/call callback funcs accross threads
(assuming functions are written so as note to alter state of calee)

As long as the callback registration mechanism is thread safe, then yes.

-cd
 
B

Bit byte

Carl said:
LoadLibrary doesn't "run the executable" when loading a DLL, except for the
DllMain function. That IS run in the context of the thread that called
LoadLibrary. Subsequent calls to fuinctions in the DLL will occur in
whatever thread(s) make the call(s) - there's no hidden thread switch back
to the thread that loaded the DLL or anything like that (in contrast with,
for example, calling into a STA-threaded COM object, where such a thread
switch does indeed occur).




As long as the callback registration mechanism is thread safe, then yes.

-cd
Thanks for the clarification
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top