Solved

Multithreaded programming

Posted on 2003-10-30
8
147 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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…
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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

19 Experts available now in Live!

Get 1:1 Help Now