Thread_test.cc revision cd139dc7
1cd139dc7SShuo Chen#include "../Thread.h"
2cd139dc7SShuo Chen
3cd139dc7SShuo Chen#include <string>
4cd139dc7SShuo Chen#include <boost/bind.hpp>
5cd139dc7SShuo Chen#include <stdio.h>
6cd139dc7SShuo Chen
7cd139dc7SShuo Chenvoid threadFunc()
8cd139dc7SShuo Chen{
9cd139dc7SShuo Chen  printf("tid=%d\n", muduo::CurrentThread::tid());
10cd139dc7SShuo Chen}
11cd139dc7SShuo Chen
12cd139dc7SShuo Chenvoid threadFunc2(int x)
13cd139dc7SShuo Chen{
14cd139dc7SShuo Chen  printf("tid=%d, x=%d\n", muduo::CurrentThread::tid(), x);
15cd139dc7SShuo Chen}
16cd139dc7SShuo Chen
17cd139dc7SShuo Chenclass Foo
18cd139dc7SShuo Chen{
19cd139dc7SShuo Chen public:
20cd139dc7SShuo Chen  explicit Foo(double x)
21cd139dc7SShuo Chen    : x_(x)
22cd139dc7SShuo Chen  {
23cd139dc7SShuo Chen  }
24cd139dc7SShuo Chen
25cd139dc7SShuo Chen  void memberFunc()
26cd139dc7SShuo Chen  {
27cd139dc7SShuo Chen    printf("tid=%d, Foo::x_=%f\n", muduo::CurrentThread::tid(), x_);
28cd139dc7SShuo Chen  }
29cd139dc7SShuo Chen
30cd139dc7SShuo Chen  void memberFunc2(const std::string& text)
31cd139dc7SShuo Chen  {
32cd139dc7SShuo Chen    printf("tid=%d, Foo::x_=%f, text=%s\n", muduo::CurrentThread::tid(), x_, text.c_str());
33cd139dc7SShuo Chen  }
34cd139dc7SShuo Chen
35cd139dc7SShuo Chen private:
36cd139dc7SShuo Chen  double x_;
37cd139dc7SShuo Chen};
38cd139dc7SShuo Chen
39cd139dc7SShuo Chenint main()
40cd139dc7SShuo Chen{
41cd139dc7SShuo Chen  printf("pid=%d, tid=%d\n", ::getpid(), muduo::CurrentThread::tid());
42cd139dc7SShuo Chen
43cd139dc7SShuo Chen  muduo::Thread t1(threadFunc);
44cd139dc7SShuo Chen  t1.start();
45cd139dc7SShuo Chen  t1.join();
46cd139dc7SShuo Chen
47cd139dc7SShuo Chen  muduo::Thread t2(boost::bind(threadFunc2, 42),
48cd139dc7SShuo Chen                   "thread for free function with argument");
49cd139dc7SShuo Chen  t2.start();
50cd139dc7SShuo Chen  t2.join();
51cd139dc7SShuo Chen
52cd139dc7SShuo Chen  Foo foo(87.53);
53cd139dc7SShuo Chen  muduo::Thread t3(boost::bind(&Foo::memberFunc, &foo),
54cd139dc7SShuo Chen                   "thread for member function without argument");
55cd139dc7SShuo Chen  t3.start();
56cd139dc7SShuo Chen  t3.join();
57cd139dc7SShuo Chen
58cd139dc7SShuo Chen  muduo::Thread t4(boost::bind(&Foo::memberFunc2, boost::ref(foo), std::string("Shuo Chen")));
59cd139dc7SShuo Chen  t4.start();
60cd139dc7SShuo Chen  t4.join();
61cd139dc7SShuo Chen
62cd139dc7SShuo Chen  printf("number of created threads %d\n", muduo::Thread::numCreated());
63cd139dc7SShuo Chen}
64