?
Solved

Multithreaded programming

Posted on 2003-10-30
8
Medium Priority
?
158 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:CindyZhou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9650874
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
0
 
LVL 13

Expert Comment

by:SteH
ID: 9650915
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.
0
 

Author Comment

by:CindyZhou
ID: 9651035
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:SteH
ID: 9651161
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.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 1000 total points
ID: 9651169
The header file is being included in a number of cpp files and I suspect that is making the compiler object as you have an instance of a variable declared.
I think you should declare it in the cpp file and have it as an extern variable in the other cpp files.  That should allow it to compile without errors.
0
 

Author Comment

by:CindyZhou
ID: 9651316
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


0
 
LVL 13

Expert Comment

by:SteH
ID: 9651425
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.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9651595
The method with the extern keyword is how one should declare 'global' vars for an app.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

718 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