Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Threads with C++

Posted on 1997-04-07
1
Medium Priority
?
660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
dtowell earned 100 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 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