Solved

Create Thread

Posted on 1998-09-09
11
700 Views
Last Modified: 2013-12-14
hi,
I've created a Win 32 console application.
I have the following files:

alertmgr.h
----------
DWORD ProcessQueue();

alertmgr.cpp
------------
DWORD AlertManager::ProcessQueue()
{
....}

main.cpp
--------
void main()
{
  HANDLE hThread;
  AlertManager *mgr = new AlertManager;

  hThread = CreateThread (NULL,0,
           (LPTHREAD_START_ROUTINE)mgr->ProcessQueue,
            NULL, 0, &dwThread);
   .....
}

When I compile, I get the following errors:
C:\ESPdev\bin\AlertMgt\main.cpp(141) : error C2440: 'type cast' : cannot convert from 'overloaded function type' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type

However, if I put Process Queue in main.cpp, there are no errors.

Does it mean that all functions for a thread should be local to the file that creates the thread?

If I need to call a function from another class, how can I go about doing it?

Please advise.
Thanks,
Andrea

0
Comment
Question by:tanmh
[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
  • 6
  • 5
11 Comments
 
LVL 7

Accepted Solution

by:
faster earned 70 total points
ID: 1172386
The function you specify in CreateThread must be a normal C function, not C++ function.  This is because a C++ function actually needs one more parameter: this.  So in your thread you can  access a C++ function, but the startup function itself has to be a C function, normally you can pass aq struct pointer as the parameter for the startup function and in the struct you can include a pointer to some class instance.
0
 

Author Comment

by:tanmh
ID: 1172387
hi "faster",
thanks for your reply but I'm still unclear.
does it mean that for my case, if i want to use mgr->ProcessQueue, my CreateThread function shall be
CreateThread(NULL,0,
              (LPTHREAD_START_ROUTINE)mgr->ProcessQueue,
               this, 0, &dwThread);

pls advise.
thanks.
Andrea
0
 
LVL 7

Expert Comment

by:faster
ID: 1172388
No, a cast won't work.  You need to have a knowledge of the different calling convention to fully understand this.  Anyway, it must be a REAL c function, for example, you define a normal C function, processqueue,

DWORD processqueue(void *ptr)
{
AlertManager *mgr = (AlertManager *) ptr;
ptr->ProcessQueue();
.
}

0
Independent Software Vendors: 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!

 

Author Comment

by:tanmh
ID: 1172389
thanks.
it works.
0
 

Author Comment

by:tanmh
ID: 1172390
hi,
I have another enquiry..
in the above example,
alertmgr.cpp
------------
   DWORD processqueue()
   {
     AlertQ* inq = pentmgr->GetInQ();
     DisplayInQ(*inq);       ----------(1)
     ....
    }

   main.cpp
   --------
   void main()
   {
     HANDLE hThread;
     AlertManager *mgr = new AlertManager;

     AlertQ* inq = pentmgr->GetInQ();

     // add items into the linked list
     AddInQ(*inq, .....);
     DisplayInQ(*inq); -------------- (2)

     hThread = CreateThread (NULL,0,
              (LPTHREAD_START_ROUTINE)processqueue,
               NULL, 0, &dwThread);
      .....
   }

At (2) when i print the contents of the inq, I can see the data.
however, when I tried to print at (1), the list is empty.
Is this because the thread can't share the process's memory?
What must I do to ensure my thread and process can read/write the
shared data, e.g. linked lists?

Thanks.
Andrea

p.s I'm willing to add points to this qn, if required.
0
 
LVL 7

Expert Comment

by:faster
ID: 1172391
where is pentmgr defined?  And how do you implement GetInQ()?
0
 

Author Comment

by:tanmh
ID: 1172392
To answer your questions:

class EntityManager {
private:
            AlertInQ      InQ;
public:
            // Constructor
            EntityManager() {};
            // Destructor
            ~EntityManager();

            // list operations
            AlertInQ* GetInQ() {return &InQ;}
};
and pentmgr is a global pointer.
EntityManager *pentmgr = new EntityManager();

I've checked thru with Purify for WinNT and there were no memory leaks anywhere.


0
 
LVL 7

Expert Comment

by:faster
ID: 1172393
check in debug wndow whether the value for inq in the two cases are the same
0
 

Author Comment

by:tanmh
ID: 1172394
I've checked through and the address for inq before the thread creation and in the thread function is the same. However, the contents of the inq is corrupted. InQ stores nodes of AlertInN class.

class AlertInN
{
private:
      int type;
      int machine;
      unsigned short indx1;
      unsigned short indx2;
      unsigned short indx3;
public:...
};
in addition, I get the following errors in the debug window, especially when there's cout statements
Loaded 'C:\WINNT\system32\KERNEL32.DLL', no matching symbolic information found.
Loaded 'C:\Program Files\Rational\Purify\Cache\!PKERNEL.DLL', no matching symbolic information found.
First-chance exception in AlertMgt.exe (KERNEL32.DLL): 0xC0000005: Access Violation.
0
 
LVL 7

Expert Comment

by:faster
ID: 1172395
The problem probably is caused by some other part of your code, not relating to this thread
0
 

Author Comment

by:tanmh
ID: 1172396
thanks for your help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
c++ getting the first 10 characters of a char* string 11 111
gdb doesn't stop on breakpoint 2 140
What is sub-make ? 2 107
DCT of  2D array using fftw in c++ 9 134
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

739 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