把类成员函数封装成线程API所需要的函数
2014-09-10 10:13
246 查看
template<class _Ty>
void (*mem_fun_thread_t(void (_Ty::* mem_fun)()))(void*){
union{
void (*_start_address)(void*);
void (_Ty::* _mem_fun)();
}thread_func;
thread_func._mem_fun = mem_fun;
return thread_func._start_address;
}
#include "stdafx.h"
#include <string>
#include <iostream>
#include <conio.h>
#include <process.h>
using namespace std;
class Hello
{
public:
Hello(const std::string& name):m_name(name){
}
~Hello(){
}
void run(){
cout << "Hello " << m_name << endl;
}
private:
std::string m_name;
};
template<class _Ty>
void (*mem_fun_thread_t(void (_Ty::* mem_fun)()))(void*){
union{
void (*_start_address)(void*);
void (_Ty::* _mem_fun)();
}thread_func;
thread_func._mem_fun = mem_fun;
return thread_func._start_address;
}
int _tmain(int argc, _TCHAR* argv[])
{
Hello hello("world");
_beginthread(mem_fun_thread_t(&Hello::run), 0, &hello);
_getch();
return 0;
}
void (*mem_fun_thread_t(void (_Ty::* mem_fun)()))(void*){
union{
void (*_start_address)(void*);
void (_Ty::* _mem_fun)();
}thread_func;
thread_func._mem_fun = mem_fun;
return thread_func._start_address;
}
#include "stdafx.h"
#include <string>
#include <iostream>
#include <conio.h>
#include <process.h>
using namespace std;
class Hello
{
public:
Hello(const std::string& name):m_name(name){
}
~Hello(){
}
void run(){
cout << "Hello " << m_name << endl;
}
private:
std::string m_name;
};
template<class _Ty>
void (*mem_fun_thread_t(void (_Ty::* mem_fun)()))(void*){
union{
void (*_start_address)(void*);
void (_Ty::* _mem_fun)();
}thread_func;
thread_func._mem_fun = mem_fun;
return thread_func._start_address;
}
int _tmain(int argc, _TCHAR* argv[])
{
Hello hello("world");
_beginthread(mem_fun_thread_t(&Hello::run), 0, &hello);
_getch();
return 0;
}
相关文章推荐
- 线程的封装技巧:把任意类的成员函数指针作为参数
- 任意类成员函数作为线程参数的封装
- 线程的封装技巧:把任意类的成员函数指针作为参数
- 任意类成员函数作为线程参数的封装ex
- 如何让API回调你的VC类成员函数而不是静态函数
- SystemParametersInfo API 函数,此功能的状态可以被选中,启用,或禁用根据需要
- 一些存储概念自己有用需要封装内存分配函数是了解
- 如何实现利用类成员函数创建线程
- 在单独线程中执行对象成员函数
- 一种在线程函数里面访问类成员的方法
- C++中线程函数中方访问成员变量的问题
- 如何实现利用类成员函数创建线程
- 不使用汇编在vc中转换类的成员函数为一个线程的通用代码
- 编程经验点滴(四)——《使用类的成员函数作为线程的执行函数》
- 如何实现类的成员函数创建线程
- 如何实现类的成员函数创建线程
- 线程函数如何共享类成员
- 如何实现利用类成员函数创建线程 选择自 iceezone 的 Blog
- 一个人的战争(2) : 成员函数与线程
- 将线程函数做成成员函数的方法