转自: http://qtlinux.blog.51cto.com/3052744/1698951
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include <iostream>
class
HelloRunnable:
public
Poco::Runnable
{
virtual
void
run()
{
std::cout <<
"Hello, world!"
<< std::endl;
}
};
int
main(
int
argc,
char
** argv)
{
HelloRunnable runnable;
Poco::Thread
thread
;
thread
.start(runnable);
//传入对象而不是对象指针
thread
.join();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include "Poco/Thread.h"
#include "Poco/RunnableAdapter.h"
#include <iostream>
class
Greeter
{
public
:
void
greet()
{
std::cout <<
"Hello, world!"
<< std::endl;
}
};
int
main(
int
argc,
char
** argv)
{
Greeter greeter;
Poco::RunnableAdapter<Greeter> runnable(greeter, &Greeter::greet);
Poco::Thread
thread
;
thread
.start(runnable);
thread
.join();
//等待该线程技术
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include <iostream>
#include "Poco/Thread.h"
#include "Poco/ThreadLocal.h"
#include "Poco/Runnable.h"
using
namespace
std;
using
namespace
Poco;
void
sayHello(
void
* name)
{
cout<<
"Hello "
<<(
char
*)name<<endl;
}
int
main()
{
static
char
* name =
"DJWu"
;
Thread thr;
thr.start(sayHello, name);
thr.join();
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include "Poco/ThreadLocal.h"
#include <iostream>
class
Counter:
public
Poco::Runnable
{
void
run()
{
static
Poco::ThreadLocal<
int
> tls;
for
(*tls = 0; *tls < 10; ++(*tls))
{
std::cout << *tls << std::endl;
}
}
};
int
main(
int
argc,
char
** argv)
{
Counter counter;
Poco::Thread t1;
Poco::Thread t2;
t1.start(counter);
t2.start(counter);
t1.join();
t2.join();
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#include <iostream>
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include "Poco/ThreadTarget.h"
#include "Poco/RunnableAdapter.h"
using
namespace
std ;
using
Poco::Thread;
using
Poco::Runnable;
using
Poco::ThreadTarget;
using
Poco::RunnableAdapter;
//传入对象
class
MyRunnable:
public
Runnable
{
public
:
void
run() { std::cout <<
"hello MyRunnable."
<< std::endl; }
};
//传入函数
void
gFun4Td()
{
std::cout <<
"hello gFun4Td"
<< std::endl;
}
//直接传入类中的函数
class
staticFun4Td
{
public
:
static
void
staticFun() { std::cout <<
"hello static fun."
<< std::endl; }
};
class
commFun4Td
{
public
:
void
commFun() { std::cout <<
"hello common function."
<< std::endl; }
};
int
main()
{
Thread t1(
"MyRun"
);
Thread t2(
"global"
);
Thread t3(
"static"
);
Thread t4(
"comm"
);
MyRunnable rMy;
ThreadTarget rg(gFun4Td);
ThreadTarget rs(&staticFun4Td::staticFun);
commFun4Td com;
RunnableAdapter<commFun4Td> rc(com,&commFun4Td::commFun);
t1.start(rMy);
Thread::sleep(100);
t2.start(rg);
Thread::sleep(100);
t3.start(rs);
Thread::sleep(100);
t4.start(rc);
t1.join();
t2.join();
t3.join();
t4.join();
return
0;
}
|