Solved

Multithreaded programming

Posted on 2003-10-30
8
146 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 44

Accepted Solution

by:
AndyAinscow earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The method with the extern keyword is how one should declare 'global' vars for an app.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now