这里有一个有趣的思想实验得到一个单独的线程中执行的功能。void doit( int i ) {
printf( "Hello from doit! i: %d\n", i );
}
void doit2( Thread* th, int i ) {
printf( "Hello from doit2! i: %d, th: %p, tid: 0x%04x \n",
i, th, th->getThreadID() );
}
class Snarfy {
public:
void thisBlows( int g ) {
printf( "Hello from thisBlows! i: %d, this ptr: %p\n", g, this );
}
};
class Swanky {
public:
void doit( double& d, const String& s ) {
printf( "Hello from Swanky::doit! d: %0.3f, s: %s, this ptr: %p\n",
d, s.ansi_c_str(), this );
}
void doit2( Thread* th, double& d, const String& s ) {
printf( "Hello from Swanky::doit! d: %0.3f, s: %s, this ptr: %p\n",
d, s.ansi_c_str(), this );
for (int i=0;i<10;i++){
th->sleep(1000);
}
}
};
int main( int argc, char** argv ){
FoundationKit::init( argc, argv );
Thread* th = ThreadedProcedure1<int>(10,doit);
th->wait();
th = ThreadedProcedure1<int>(231,doit2);
th->wait();
Snarfy sn;
th = ThreadedProcedure1<int,Snarfy >(&sn,38112,&Snarfy::thisBlows);
th->wait();
String s = "hello";
Swanky sk;
double d = 0.0332;
th = ThreadedProcedure2< double&,const String&,
Swanky >(&sk,d,s,&Swanky::doit2);
th->wait();
printf( "Bye!\n");
FoundationKit::terminate();
return 0;
实际的实现变得有点长篇大论,但看起来像这样:{C}
这使我们能够附加功能,并在一个单独的线程执行它,使用各种VCF的线程类,如来实现它。我会离开它作为一个运动,为读者增加额外的参数。如果像这样的人,足够我们可能会投入适当的FoundationKit本。|吉姆・克拉夫