下面的代码是如何使用:testthread.cpp
view plaincopy to clipboardprint?
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <sys/time.h>
#include <string>
#include "blmutex.h"
#include "basethread.h"
using namespace std;
BL_Mutex am;
void *thread1 (void* data )
{
for ( int i = 0; i < 10; i ++ )
{
BL_Lock bl ( &am );
usleep(1000);
cout << "in thread" << endl;
}
return (void*)0;
};
class CMyThread : public CBaseThread
{
public:
virtual bool Init() { return true; };
virtual void Runonce()
{
{
cout << "In CMyThread" << endl;
usleep(100);
}
};
virtual void Clear() {};
};
int main ( int argc, char *argv [] )
{
string str;
str = "hello world";
cout << str << endl;
pthread_t t;
pthread_create ( &t, NULL, thread1, NULL );
CMyThread mythread;
mythread.start("MyThread");
for ( int i = 10; i < 20; i ++ )
{
BL_Lock bl ( &am );
usleep(400);
cout << "in main " << endl;
}
pthread_join ( t, NULL );
mythread.end();
cout << "exit" << endl;
return 0;
}
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <sys/time.h>
#include <string>
#include "blmutex.h"
#include "basethread.h"
using namespace std;
BL_Mutex am;
void *thread1 (void* data )
{
for ( int i = 0; i < 10; i ++ )
{
BL_Lock bl ( &am );
usleep(1000);
cout << "in thread" << endl;
}
return (void*)0;
};
class CMyThread : public CBaseThread
{
public:
virtual bool Init() { return true; };
virtual void Runonce()
{
{
cout << "In CMyThread" << endl;
usleep(100);
}
};
virtual void Clear() {};
};
int main ( int argc, char *argv [] )
{
string str;
str = "hello world";
cout << str << endl;
pthread_t t;
pthread_create ( &t, NULL, thread1, NULL );
CMyThread mythread;
mythread.start("MyThread");
for ( int i = 10; i < 20; i ++ )
{
BL_Lock bl ( &am );
usleep(400);
cout << "in main " << endl;
}
pthread_join ( t, NULL );
mythread.end();
cout << "exit" << endl;
return 0;
}
就像testthread.cpp中的一样,从CBaseThread继承来的一个CMyThread,重写了Init,Runonce,Clear三个函数,就可以用了。线程的属性是用的默认属性,其它属性设置还没有完成,不过,这个类现在可以用了。
