Solved

Multithreaded programming

Posted on 2003-10-30
8
150 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 250 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 55
post4 challenge 28 102
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 95
how to split multiple lines delimiter : 8 100
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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