Solved

Create Thread

Posted on 1998-09-09
11
697 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
  • 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
 

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

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

920 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

16 Experts available now in Live!

Get 1:1 Help Now