多线程和成员函数 [英] multithreading and member-functions
问题描述
我可以使用_beginthread()运行一个线程,并在类的成员函数上作为第一个参数传递
指针吗?
#include < process.h>
A级
{
void _cdecl f(void *){};
};
void main()
{
_beginthread(& A :: f,0 ,NULL);
}
我收到编译错误:
错误C2664:''_ beginthread'':Konvertierung des参数1 von''void
(__ cdecl A :: *)(void *)''in''void(__ cdecl *)(void *)''nicht moglich
>
PS。我在MSVC7.0(德语版:),控制台应用程序工作
Can I run a thread with _beginthread() and pass as the first parameter the
pointer on a member function of a class?
#include <process.h>
class A
{
void _cdecl f(void*) {};
};
void main()
{
_beginthread(&A::f, 0, NULL);
}
I get the compilation error:
error C2664: ''_beginthread'' : Konvertierung des Parameters 1 von ''void
(__cdecl A::* )(void *)'' in ''void (__cdecl *)(void *)'' nicht moglich
PS. I work in MSVC7.0 (german version :), console application
推荐答案
" Ilia Poliakov" < IP ***** @ allesklar.de>在消息中写道
news:bj ************ @ ID-120622.news.uni-berlin.de ...
"Ilia Poliakov" <ip*****@allesklar.de> wrote in message
news:bj************@ID-120622.news.uni-berlin.de...
可以我用_beginthread()运行一个线程,并将一个参数作为第一个参数传递给一个类的成员函数?
#include< process.h>
A级
{
void _cdecl f(void *){};
};
void main()
{
_beginthread(& A :: f,0,NULL);
}
我收到编译错误:
错误C2664:''_ bebethread'':Konvertierung des参数1 von''void
(__ cdecl A :: *)(void *)''in''void(__ cdecl *)(void *)''nicht moglich
PS。我在MSVC7.0(德语版:),控制台应用程序中工作
Can I run a thread with _beginthread() and pass as the first parameter the
pointer on a member function of a class?
#include <process.h>
class A
{
void _cdecl f(void*) {};
};
void main()
{
_beginthread(&A::f, 0, NULL);
}
I get the compilation error:
error C2664: ''_beginthread'' : Konvertierung des Parameters 1 von ''void
(__cdecl A::* )(void *)'' in ''void (__cdecl *)(void *)'' nicht moglich
PS. I work in MSVC7.0 (german version :), console application
1.如果你在Windows NG上问这个会更好。
2.请在发布时将错误翻译成英文。
然后可能有人在这里可以帮助你。
-
随着祝福,
J.Schafer
1. It would be better if you ask this on a Windows NG.
2. Please translate the errors to English whenever posting.
Then may be someone here can help you.
--
With best wishes,
J.Schafer
1.如果你在Windows上问这个会更好NG。
_beginthread()是一个C运行时函数,与任何操作系统都没有任何关系。
2.请在发布时将错误翻译成英文。<然后可能有人在这里可以帮助你。
1. It would be better if you ask this on a Windows NG.
_beginthread() is a C run-time function and has no relationship to any OS.
2. Please translate the errors to English whenever posting.
Then may be someone here can help you.
错误C2664:''_ bebethread'':参数1的转换来自''void(__ cdecl
A :: *)(void *)''''void(__ cdecl *)(void *)''不可能
error C2664: ''_beginthread'' : convertion of parameter 1 from ''void (__cdecl
A::* )(void *)'' in ''void (__cdecl *)(void *)'' not possible
Ilia Poliakov写道:
Ilia Poliakov wrote:
我可以用_beginthread()运行一个线程,并将指针作为第一个
参数传递给类的成员函数吗?
Can I run a thread with _beginthread() and pass as the first
parameter the pointer on a member function of a class?
不。 AFAIK _beginthread是一个C函数,带有一个void *参数。
指针和指向成员的指针是非常不同的野兽。指向
成员(函数或变量)的指针可以具有与普通指针相比非常不同的实现。标准(让我不要查阅)明确指出
这种转换(来自和来自)不受支持。恕我直言,你可以做的最好是
来创建一个包含指向成员的指针的小类,创建它(
静态/全局,动态分配或其他一些方法确保当新线程试图访问它时,仍然存在
)并将指针传递给
这个非常参数列表到新线程。当然,如果你需要更多的
参数,你可以将它们添加到这个类(或结构)中。
-
Attila又名WW
Nope. AFAIK _beginthread is a C function, taking a void * argument.
Pointers and pointers to members are very different beasts. A pointer to a
member (function or variable) can have a very different implementation than
a normal pointer. The standard (let me not look it up) clearly states that
such conversion (from and to) is not supported. IMHO the best you can do is
to make a little class containing that pointer to member, create it (either
static/global, dynamicly allocated or some other way that ensures that it is
still there when the new thread tries to access it) and pass a pointer to
this very "argument list" to the new thread. Of course if you need more
arguments you can add them into this class (or struct).
--
Attila aka WW
这篇关于多线程和成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!