Solved

Create Thread

Posted on 1998-09-09
11
699 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 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