Solved

Threads with C++

Posted on 1997-04-07
1
655 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

713 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