How to get Windows thread pool to call class member function?

I want the Windows thread pool (QueueUserWorkItem()) to call my class' member functions.

Unfortunately this cannot be done directly by passing a member function pointer as an argument to QueueUserWorkItem().

What makes it difficult is that more than one member function must be callable and they have different signatures (all return void though).

One probably need to add a few layers of abstraction to get this to work, but I'm not sure how to approach this. Any ideas?


This might help. You can use tr1::function () and tr1::bind to "coalesce" various calls:

#include <iostream> #include <tr1/functional> using namespace std; using namespace tr1; class A { public: void function(int i) { cout << "Called A::function with i=" << i << endl; } }; void different_function(double c) { cout << "Called different_function with c=" << c << endl; } int main(int argc, char* argv[]) { function<void()> f = bind(different_function, 3.14165); f(); A a; f = bind(&A::function, a, 10); f(); return 0; }

The address of the function object can be passed as a single callable object (needing only one address).


Example: In your class add:

char m_FuncToCall; static DWORD __stdcall myclass::ThreadStartRoutine(LPVOID myclassref) { myclass* _val = (myclass*)myclassref; switch(m_FuncToCall) { case 0: _val->StartMyOperation(); break; } return 0; }

Make a member for adding to queue then

void myclass::AddToQueue(char funcId) { m_FuncToCall=funcId; QueueUserWorkItem(ThreadStartRoutine,this,WT_EXECUTEDEFAULT); }

or create

typedef void (*MY_FUNC)(void); typedef struct _ARGUMENT_TO_PASS { myclass* classref; MY_FUNC func; }ARGUMENT_TO_PASS;

and then

void myclass::AddToQueue(MY_FUNC func) { ARGUMENT_TO_PASS _arg; _arg.func = func; _arg.classref = this; QueueUserWorkItem(ThreadStartRoutine,&_arg,WT_EXECUTEDEFAULT); }

If you need further explanation feel free to ask :)

EDIT:</b> You'll need to change the ThreadStartRoutine for the second example and you can also change the struct to hold the passing argument


  • conversion of array to string
  • what's the size of std::tr1::array >
  • Constant-sized vector
  • Boost Serialization - Serialize std::tr1::shared_ptr?
  • Modify the content of a std container through a different container
  • Getting java.lang.IllegalStateException: This call must happen in the AWT Event Dispatch Thread! Ple
  • Pyspark window function with condition
  • Spark dataframes: Extract a column based on the value of another column
  • Find exception hiding/swallowing in C# code in VS2013
  • Changing NULL's position in sorting
  • How do I conditionally select a field from one of two tables?
  • What is wrong with this emulation of CMPXCHG16B instruction?
  • How do I fix “The program issued a command but the command length is incorrect.” error when calling
  • C++ String tokenisation from 3D .obj files
  • Get the pasted content on document on paste event
  • Knockout custom binding handler
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • jQuery Orbit - How to make a Random Slideshow?
  • What and where is mdimport
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Meteor helpers not available in Angular template
  • jQuery show() function is not executed in Safari if submit handler returns true
  • Get object from AWS S3 as a stream
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • PHP: When would you need the self:: keyword?
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • Is there any way to bind data to data.frame by some index?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal