作者:龙飞
SDL创建多线程的函数SDL_CreateThread()所调用的是函数指针,这意味着我们不可以传入(非静态)成员函数的指针。关于两种函数指针我们之前已经讨论过:函数指针与成员函数指针,我们可以有两种方法能让具有普通函数指针(函数指针以及静态成员函数指针)的函数调用类的私有成员,一是友元函数,另外就是静态成员函数。而能够受到类私有保护的,只有静态成员函数。所以,我们可以通过静态成员函数调用一个对象数据的形式,实现对于创建多线程函数的封装。
另外,我们希望测试在主线程中读写线程数据的效果,所以添加了两个方法show() 和reset(),多线程演示的类源代码如下:
#include
<
iostream
>
#include " SurfaceClass.hpp "
class AmnArg
{
private :
int beginX;
int beginY;
int endX;
int endY;
const ScreenSurface & screen;
//
static int amn( void * pThat);
public :
AmnArg( int begin_x, int begin_y, int end_x, int end_y, const ScreenSurface & _screen);
SDL_Thread * createThrd();
void show() const ;
void reset();
};
#include " SurfaceClass.hpp "
class AmnArg
{
private :
int beginX;
int beginY;
int endX;
int endY;
const ScreenSurface & screen;
//
static int amn( void * pThat);
public :
AmnArg( int begin_x, int begin_y, int end_x, int end_y, const ScreenSurface & _screen);
SDL_Thread * createThrd();
void show() const ;
void reset();
};
其中SurfaceClass.hpp请参考:
http://www.cppblog.com/lf426/archive/2008/04/14/47038.html
实现函数如下:
#include
"
amn.hpp
"
AmnArg::Am
AmnArg::Am