日期:2014-05-16 浏览次数:20598 次
/*
* filename: test.cpp
*/
#include <iostream>
#include <pthread.h>
class task
{
public:
explicit task(int times) : echo_times_(times)
{
}
void active_thread()
{
pthread_t tid;
::pthread_create(&tid, NULL, task::thread_func, this);
::pthread_join(tid, NULL);
}
private:
static void* thread_func(void *param)
{
task *self = (task*)param;
for (int i = 0; i < self->echo_times_; ++i)
std::cout << "echo times: " << i + 1 << std::endl;
return NULL;
}
private:
int echo_times_;
};
int main()
{
task(5).active_thread();
return 0;
}
g++ test.cpp -lpthread
[coderchen@self thread]$ ./a.out
echo times: 1
echo times: 2
echo times: 3
echo times: 4
echo times: 5