关于_beginthreadex()

jackzhxy 2011-09-23 03:23:29
我封装了一个类class A。A里面有方法B和C

在方法B里我调用了_beginthreadex(),函数入口为C。

但是编译报错:
error C3867: “A::C”: 函数调用缺少参数列表;请使用“&A::C”创建指向成员的指针

当然我可以将C设定为static的,但是问题又来了,类A里的其他函数和成员变量都要求使用static,如果我都改成了static,编译器还是报错。


请问该如何解决?
...全文
714 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackzhxy 2011-09-27
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 huayehanshan 的回复:]
C/C++ code

class CxxSocket
{
public:
CxxSocket();
~CxxSocket();
private:
static UINT TCPServer_RecvProc(LPVOID lParam);
.......
}

BOOL CxxSocket::TCP_Server_Start()
{

...……
[/Quote]

产生的那么多句柄不是beginthread的?是beginthread调用的过程产生的那么多句柄。

我实际测试过了,我用beginthreadex()调用一个不会产生句柄的过程,在begin之前和之后分别输出一下当前程序的句柄数,发现差值是8,我想肯定都是beginthread产生的把。
叶落寒山 2011-09-27
  • 打赏
  • 举报
回复

class CxxSocket
{
public:
CxxSocket();
~CxxSocket();
private:
static UINT TCPServer_RecvProc(LPVOID lParam);
.......
}

BOOL CxxSocket::TCP_Server_Start()
{

....
AfxBeginThread(TCPServer_RecvProc,this);
....
}

UINT CxxSocket::TCPServer_RecvProc(LPVOID lParam)
{
CxxSocket * pMySock = (CxxSocket*)lParam;

pMySock->.....
}
jackzhxy 2011-09-27
  • 打赏
  • 举报
回复
多线程的句柄数控制?

_beginthreadex()后,每begin一次系统产生8个句柄最后稳定下来是10个。

但是产生的句柄只能closehandle一次。那么多的句柄什么时候才释放?怎么样才能让她们释放?

纠结中,往大大们给支个招。
jackzhxy 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ljljlj 的回复:]
按此法传入类的指针。

C/C++ code
//vc6.0 cmd:
//cl /MT /GX test.cpp
#include <iostream>
#include <process.h>
#include <windows.h>
class A{
public:
int i;
static unsigned __stdcall C(void *pParam){
……
[/Quote]

hThread = (HANDLE)::_beginthreadex( NULL, 0, C, (LPVOID)this, 0, &threadID );


我不理解,怎么是第四个参数,函数参数变成this了呢
jackyjkchen 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jackyjkchen 的回复:]
beginthreadex要传入this指针

静态函数没自己有this,你所有的调用成员都用this调
[/Quote]

.
jackzhxy 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jackyjkchen 的回复:]
m_iCurrentCupsNo
Requst
都是成员函数,必须用this调用

前面都说过了
[/Quote]

if(0 !=(iRet = this->Requst("BatchSettltment", &reqmsg, &rspmsg, 0)) )
this->m_iCurrentCupsNo = this->m_iCurrentCupsNo + 1;


1>.\VRouterSwitch.cpp(756) : error C2671: “VRSwitch::TP_BatchSettltmentProc”: 静态成员函数没有“this”指针
1>.\VRouterSwitch.cpp(756) : error C2227: “->m_iCurrentCupsNo”的左边必须指向类/结构/联合/泛型类型
1>.\VRouterSwitch.cpp(756) : error C2671: “VRSwitch::TP_BatchSettltmentProc”: 静态成员函数没有“this”指针
1>.\VRouterSwitch.cpp(756) : error C2227: “->m_iCurrentCupsNo”的左边必须指向类/结构/联合/泛型类型
1>.\VRouterSwitch.cpp(776) : error C2671: “VRSwitch::TP_BatchSettltmentProc”: 静态成员函数没有“this”指针
1>.\VRouterSwitch.cpp(776) : error C2227: “->Requst”的左边必须指向类/结构/联合/泛型类型
ljhhh0123 2011-09-23
  • 打赏
  • 举报
回复
按此法传入类的指针。
//vc6.0 cmd:
//cl /MT /GX test.cpp
#include <iostream>
#include <process.h>
#include <windows.h>
class A{
public:
int i;
static unsigned __stdcall C(void *pParam){
std::cout << "hello\n" <<((A*)pParam)->i<< std::endl;
return 0;
}
void B(){
i=100;
HANDLE hThread;
unsigned threadID;
hThread = (HANDLE)::_beginthreadex( NULL, 0, C, (LPVOID)this, 0, &threadID );

}
};
int main(){
A a;
a.B();while(1);
return 0;
}
ljhhh0123 2011-09-23
  • 打赏
  • 举报
回复
//vc6.0 cmd:
//cl /MT /GX test.cpp
#include <iostream>
#include <process.h>
#include <windows.h>
class A{
public:

static unsigned __stdcall C(void *pParam){
std::cout << "hello\n" << std::endl;
return 0;
}
void B(){
HANDLE hThread;
unsigned threadID;
hThread = (HANDLE)::_beginthreadex( NULL, 0, C, (LPVOID)this, 0, &threadID );

}
};
int main(){
A a;
a.B();while(1);
return 0;
}
jackyjkchen 2011-09-23
  • 打赏
  • 举报
回复
m_iCurrentCupsNo
Requst
都是成员函数,必须用this调用

前面都说过了
jackzhxy 2011-09-23
  • 打赏
  • 举报
回复
头文件内容:

class VRSwitch
{
public:
VRSwitch(void);
~VRSwitch(void){}
int Requst(char *title, isomsg *reqmsg, isomsg *rspmsg, unsigned char mac_flag);

int TP_BatchSettltment();
static unsigned __stdcall TP_BatchSettltmentProc(LPVOID lpParam);

public:
CString m_remote_host;
int m_remote_port;
char m_szPsamNo[64];
int m_iCurrentCupsNo;
};


cpp内容:

int VRSwitch::TP_BatchSettltment()
{

//获取终端信息
int iCupsNoTotal = 1;
//iCupsNoTotal = GetCupsNoInfo( infor );
Batch_Settlement_Data *infor = new Batch_Settlement_Data[iCupsNoTotal];

m_iCurrentCupsNo = 0;

//生成多线程
while( TRUE)
{
//所有终端没处理完或当前线程没达到最大进程数要求
if ( m_iCurrentCupsNo < iCupsNoTotal && GetProcessInfo() < _MAX_THREAD )
{
HANDLE hThead = (HANDLE) _beginthreadex(0,0,TP_BatchSettltmentProc,&infor[m_iCurrentCupsNo], 0,0);
}
}
}

//批结算
unsigned __stdcall VRSwitch::TP_BatchSettltmentProc(LPVOID lpParam)
{
//code....
m_iCurrentCupsNo = m_iCurrentCupsNo + 1;
if(0 !=(iRet = Requst("BatchSettltment", &reqmsg, &rspmsg, 0)) )
{
Log("err . BatchSettltment Data Requst ");
}

}

现在就是上面的代码,编译出错:

1>.\VRouterSwitch.cpp(756) : error C2597: 对非静态成员“VRSwitch::m_iCurrentCupsNo”的非法引用
1>.\VRouterSwitch.cpp(756) : error C2597: 对非静态成员“VRSwitch::m_iCurrentCupsNo”的非法引用
1>.\VRouterSwitch.cpp(776) : error C2671: “VRSwitch::TP_BatchSettltmentProc”: 静态成员函数没有“this”指针
1>.\VRouterSwitch.cpp(776) : error C2227: “->Requst”的左边必须指向类/结构/联合/泛型类型
就想叫yoko 2011-09-23
  • 打赏
  • 举报
回复
class A
{
public:
static DWORD WINAPI Fun(LPVOID lpParameter);
void test();
};
static DWORD WINAPI A::Fun(LPVOID lpParameter)
{
A* a = (A*)lpParameter;
a->test();
}
ljhhh0123 2011-09-23
  • 打赏
  • 举报
回复
把你的类声明及实现简化一下贴上来。
jackyjkchen 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jackzhxy 的回复:]
引用 2 楼 luciferisnotsatan 的回复:
需要用static成员函数


static之后,我class A里的其他函数报告
error C2352: “A::D_Func”: 非静态成员函数的非法调用

期中D_Func 在C_Func里被调用。
[/Quote]

beginthreadex要传入this指针

静态函数没自己有this,你所有的调用成员都用this调
就想叫yoko 2011-09-23
  • 打赏
  • 举报
回复
class A
{
public:
static DWORD WINAPI Fun(LPVOID lpParameter);
};

调用的时候
A::Fun, (LPVOID)this,

有什么错你贴错误出来
jackzhxy 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luciferisnotsatan 的回复:]
需要用static成员函数
[/Quote]

static之后,我class A里的其他函数报告
error C2352: “A::D_Func”: 非静态成员函数的非法调用

期中D_Func 在C_Func里被调用。
jackzhxy 2011-09-23
  • 打赏
  • 举报
回复
头文件里:
static unsigned __stdcall (A::*)C_Func(LPVOID lpParam);
cpp里
unsigned __stdcall (A::*)C_Func(LPVOID lpParam)
A::B_Func()
{
_beginthreadex(0,0,this->C_Func,¶, 0,0);
}

是这样吗?

编译出错,比刚才还多。。
luciferisnotsatan 2011-09-23
  • 打赏
  • 举报
回复
需要用static成员函数
就想叫yoko 2011-09-23
  • 打赏
  • 举报
回复
改成static+ 一个class A指针型参数 后 调用时传递this指针

64,635

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧