Solved

Create Thread

Posted on 1998-09-09
11
696 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks.
it works.
0
 

Author Comment

by:tanmh
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

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

Author Comment

by:tanmh
Comment Utility
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
Comment Utility
check in debug wndow whether the value for inq in the two cases are the same
0
 

Author Comment

by:tanmh
Comment Utility
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
Comment Utility
The problem probably is caused by some other part of your code, not relating to this thread
0
 

Author Comment

by:tanmh
Comment Utility
thanks for your help.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

10 Experts available now in Live!

Get 1:1 Help Now