Solved

Threads with C++

Posted on 1997-04-07
1
652 Views
Last Modified: 2013-12-03
Hi,

Using Visual C++ and the CreateThread function I have a problem with member functions.  If i call CreateThread
as follows

hconin = ::CreateThread(NULL,0,
      (LPTHREAD_START_ROUTINE)                                 WorkerThread,
      0,
      CREATE_SUSPENDED,
      &id);

Where WorkerThread is a global function everything is OK.
But if I define WorkerThread to be a member function of the Class that is calling CreateThread i get an error at compile time.  

error C2440: 'type cast' : cannot convert from 'overloaded function type' to 'unsigned long (__stdcall *)(void *)'

Anyone any ideas

Thanks

Robert

0
Comment
Question by:Robert888
1 Comment
 
LVL 1

Accepted Solution

by:
dtowell earned 50 total points
Comment Utility
Thats because a pointer to a normal member function takes a "this" pointer (changing the signature of the function).  You should use a static member function as the address passed to CreateThread(), pass the "this" pointer instead of "0", cast
the this pointer in the static function to your class type and
then call the non-static member function.  For example:

class MyThread {
  void ActualThreadFunction();
 
  static void StaticThreadFunction(void *ptr)
  {
    MyThread * myThread = reinterpret_cast<MyThread *>(ptr);
    myThread->ActualThreadFunction();
  }
 
  void Start()
  {
    // ...
    CreateThread(NULL,0,
      (LPTHREAD_START_ROUTINE)StaticThreadFunction,
      this,CREATE_SUSPENDED,&id);
    // ...
  }
};

Also, (this for free) you _really_should_ (really) use
_threadbeginex() instead of CreateThread() to avoid resource
leaks.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

9 Experts available now in Live!

Get 1:1 Help Now