65,187
社区成员




#include <iostream>
#include <process.h>
#include <windows.h>
struct AAA
{
typedef void (*T)(void *);
void Thread(void *)//禁止使用staitc
{
while (true)
{
std::cout << "+";
Sleep(250);
}
}
void Init()
{
T s = &AAA::Thread;//错误
_beginthread(s, 0, 0);
}
}aaa;
int main()
{
aaa.Init();
getchar();
}
#include <stdio.h>
#include <process.h>
#include <windows.h>
struct AAA
{
int i = 0;
uintptr_t _handle;
void Init()
{
void (__cdecl AAA::*StartAddr)() = &AAA::Thread;
void **_thread = (void **)&StartAddr;
_handle = _beginthread((_beginthread_proc_type)*_thread, 0, this);
}
void __cdecl Thread();
};
void __cdecl AAA::Thread()
{
while (1)
{
printf("thread #%p -> %d\n", (void *)_handle, i++);
Sleep(200);
}
}
int main()
{
AAA a, b;
a.Init();
Sleep(1000);
b.Init();
getchar();
return 0;
}
#include <iostream>
#include <process.h>
#include <windows.h>
struct AAA
{
int i = 0;
void Thread(void *aaa)
{
AAA* pAAA = (AAA*)aaa;
while (true)
{
std::cout << pAAA->i++;
Sleep(200);
}
}
void Init(void* pParam)
{
typedef void (AAA::*T)(void *);
T t = &AAA::Thread;
typedef void(*T1)(void *);
T1 t1 = *(T1 *)&t;
_beginthread(t1, 0, pParam);
}
}aaa;
int main()
{
AAA aaa;
aaa.Init((void*)&aaa);
getchar();
return 0;
}
#include <iostream>
#include <process.h>
#include <windows.h>
struct AAA
{
int i = 0;
void Init()
{
_beginthread(Thread, 0, 0);
}
static void Thread(void *);
}aaa;
void AAA::Thread(void *)
{
while (true)
{
std::cout << aaa.i++;
Sleep(200);
}
}
int main()
{
aaa.Init();
getchar();
}
楼上各位并没有我需要的答案,不过在此还是很感谢大家的倾情奉献啦。
在本帖子的例子中的Thread总是各种被强制static导致无法直接访问class AAA的成员对象,目前总结得到唯一办法就是把class AAA的指针用各种abcd的办法传递到Thread函数中去,但是static的结果就是Thread其实是唯一的,如果AAA aaa;完后AAA bbb;然后就bug啦,然后消除完bug后这个代码就会变得很不优雅啦,太丑了,惨不忍睹。所以楼主是希望能够消除这个static的,能够直接在Thread里面使用this这个关键词的#include <iostream>
#include <process.h>
#include <windows.h>
struct AAA
{
int i = 0;
void Thread(void *)//禁止使用staitc
{
while (true)
{
std::cout << i++;//无法访问i;
Sleep(200);
}
}
void Init()
{
typedef void (AAA::*T)(void *);
T t = &AAA::Thread;
typedef void(*T1)(void *);
T1 t1 = *(T1 *)&t;
_beginthread(t1, 0, 0);
}
}aaa;
int main()
{
aaa.Init();
getchar();
}
#include <iostream>
#include <process.h>
#include <windows.h>
struct AAA
{
int i = 0;
void Thread(void *)//禁止使用staitc
{
while (true)
{
std::cout << i++;//无法访问i;
Sleep(200);
}
}
void Init()
{
typedef void (AAA::*T)(void *);
T t = &AAA::Thread;
typedef void(*T1)(void *);
T1 t1 = *(T1 *)&t;
_beginthread(t1, 0, 0);
}
}aaa;
int main()
{
aaa.Init();
getchar();
}
2楼3楼的办法能运行,但是无法访问类成员,Thread好像被强制staitcl,3楼的C++11的线程std::thread可以使用,但是我用不到,win32里面一大堆API都是_beginthread格式的,比如SetWindowsHookEx,CreateWindowEx,WNDCLASS
#include <thread>
#include <chrono>
#include <iostream>
class Thread
{
public:
Thread(){}
~Thread()
{
stop();
}
typedef void(Thread::*Functor)();
void start()
{
Functor functor = &Thread::run;
thread = std::thread(functor, this);
}
void stop()
{
if (end)
return;
end = true;
if (thread.joinable())
thread.join();
}
private:
void run() // 禁止使用staitc
{
while (!end)
{
std::cout << "+";
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
protected:
std::thread thread;
bool end = false;
};
int main()
{
Thread thread;
thread.start();
getchar();
return 0;
}
也可以改成使用函数子,能够适配相同函数特征的函数指针、函数对象、匿名函数等