1) This is exactly what I'm encountering. I googled for that but none of those posts talked how to do this:
member function B::fb makes a call to _beginthreadex with thread function which is a member function ( A::fa ).

2) It generates error C3867: 'A::fa': function call missing argument list; use '&A::fa' to create a pointer to member.
But I don't know how to add &A:: :(
Please fix it and give me some advices.

#include<windows.h>
#include<process.h>
#include<iostream>
using std::cout;
using std::endl;
#include<vector>
using std::vector;

class A
{
	public:	
		unsigned __stdcall fa(void *param);
};

unsigned __stdcall fa(void *param)
{
	cout << (int)param << endl;
	_endthreadex(0);
	return 0;
}

class B
{
	public:	
		B();
		~B();
		void fb();

	private:
		vector<A *> a;
};

B::B()
{
	a.push_back(new A());
	a.push_back(new A());
}

B::~B()
{
	for(int i = 0; i < (int)a.size(); ++i)
		delete a[i];
}

void B::fb()
{
	int var = 0;
	_beginthreadex(NULL, 0, a[0]->fa, (void *)var, 0, NULL);
}

int main()
{
	B b;
	return 0;
}

3) And please show me how to pass a list of arguments (many values) to _beginthreadex.

I mean:

void B::fb()
{
	int var = 0;
	_beginthreadex(NULL, 0, a[0]->fa, [b]MANY_VALUES[/b], 0, NULL);
}

Thank you in advance :) .

Recommended Answers

All 2 Replies

1) This is exactly what I'm encountering. I googled for that but none of those posts talked how to do this:
member function B::fb makes a call to _beginthreadex with thread function which is a member function ( A::fa ).

Google probably doesn't talk about it because that is not possible directly. _beginthreadex takes a function pointer as the argument, not a member function pointer. The two pointer types are not compatible because function pointers are real pointers while member function pointers are just an offset into a class' member function table with pointer-like syntax. To call a member function through a function pointer callback you need to wrap the member function call in a regular function:

#include <iostream>

namespace Daniweb
{
    class Test
    {
    public:
        void f() { std::cout << "member function call\n"; }
    };

    void f(void *obj)
    {
        ((Test*)obj)->f();
    }

    void callback(void (*fp)(void *), void *obj)
    {
        (*fp)(obj);
    }
}

int main()
{
    callback(&Daniweb::f, new Daniweb::Test());
}

2) It generates error C3867: 'A::fa': function call missing argument list; use '&A::fa' to create a pointer to member.
But I don't know how to add &A::

The way member function pointers work, you need an object and the pointer. The pointer is &<class>::<function> and the object is whatever instance of <class> you have. Then they're put together with a special indirection operator:

#include <iostream>

namespace Daniweb
{
    class Test
    {
    public:
        void f() { std::cout << "member function call\n"; }
    };

    void callback(void (Test::*fp)(), Test *obj)
    {
        (obj->*fp)();
    }
}

int main()
{
    callback(&Daniweb::Test::f, new Daniweb::Test());
}

But if the callback wants a function pointer instead of a member function pointer, you can't use a member function pointer because the object is missing and the special indirection syntax isn't used by the callback.

3) And please show me how to pass a list of arguments (many values) to _beginthreadex.

The argument is a void pointer. You can pass an array using a void pointer. It is kind of kludgy, but it works if the function knows all of the types and sizes for the argument:

#include <iostream>
#include <string>

using namespace std;

namespace Daniweb
{
    void CheckArgs(void *args)
    {
        void **p = (void**)args;

        cout << *(string*)p[0] << '\n'
             << *(int*)p[1] << '\n'
             << *(double*)p[2] << '\n';
    }
}

int main()
{
    int x = 123;
    double f = 1.5;

    void *args[] =
    {
        new string("test"),
        &x,
        &f
    };

    Daniweb::CheckArgs(args);
}

Got it! Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.