Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create Thread

Posted on 1998-09-09
11
Medium Priority
?
702 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 210 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

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!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

721 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