Solved

Threads with C++

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

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…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared 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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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