Solved

Threads with C++

Posted on 1997-04-07
1
653 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
ID: 1334457
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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