Link to home
Start Free TrialLog in
Avatar of CindyZhou
CindyZhou

asked on

Multithreaded programming

Hi,

I am really stuck in this link error.

I am working on a simple multithreaded project. There are three threads involved: the main thread-a simple dialog interface, a timer thread, and a test thread. The timer thread is implemented as a timer class. The test thread does is to pop up a message when timeout. The functionality of this project is pop up a message at a certain time interval ( determined by timeout value of the timer). I declared a HANDLE as global along with the test thread function in a header file, and I did use ifdef directive in this header file. When  I build the project, I always got this link error saying the HANDLE variable is already defined.  What could go wrong here? Since it is not convenient to post the whole project code here, if someone is interested and would like to help, I could send you the complete project source.

I would appreciate any suggestion.

Cindy
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

in one cpp file use
HINSTANCE hInst;
and in the other cpp files where you require access to the hInst
extern HINSTANCE hInst;
in your cpp file instead of the h file
Avatar of Member_2_1001466
Member_2_1001466

Do you really need a global variable? I guess you get the HANDLE in the main thread. From there you can pass it to both threads. How depends on whether both of the are UI threads or just one of them.
Avatar of CindyZhou

ASKER

SteH:

Thanks for your prompt reply. You are right  and I actually did this as a work around.

But in my case, the global HANDLE would be better. Because I started my timer thread in the timer's StartTicking function, not in the main thread. So currently I have to pass the handle as an argument in the StartTicking function, which is not elegant.

And I really want to figure out why this redefinition link error occur.

Thanks,
Cindy
It occurs because the HANDLE is declared in more than 1 module (via header file?).

To get around this use the tip of AndyAinscow: In the header you can define it as extern HANDLE Hxxx; and in just one cpp file (the one of the main thread) you declare it.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SteH & AndyAinscow:

Thanks for the tip.

According to your suggestion, I declare the handle in the main thread cpp file, and use extern in every cpp file that needs it. It works now.

But I still did not understand why my problem could occur. I used ifdef directive in the .h file where the handle is declared. So if it is included in a number of cpp files, it should be OK. Plus, the error I got is a LINK error not a COMPILE error. Any idea?

Cindy


The compiler looks whether the HANDLE is defined and declared before use. It is, so he does not complain. Now several obj files are produced which all have this HANDLE declared. The linker is putting all those together to a single program. Now how should it handle several instances of an object named identical? It does not know and complains.
The method with the extern keyword is how one should declare 'global' vars for an app.