POCO C++例程整理--有关线程

转自: http://qtlinux.blog.51cto.com/3052744/1698951

本文主要整理了网上见到的,以及自己编写的有关隘poco的例子,本着开源共享的精神,供大家参考,加快poco框架库的学习和使用,加快自己在项目上的应用,加速产品的设计与开发.

 例子一: 传入对象

    在Poco中,将入口函数抽象为一个类Runnable,该类提供void run()接口,用户需要继承至该类来实现自定义的入口函数。Poco将线程也抽象为一个类Thread,提供了start, join等方法,如下:

 定义一个Thread对象,调用其start方法并传入一个Runnable对象来启动线程,使用的方法比较简单

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();
}

例子二:传入一个定义好的类中的函数

如果你的线程的入口函数在另一个已定义好的类中,那么Poco提供了一个适配器来使线程能够从你指定的入口启动,并且无需修改已有的类:

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;
}

例子三:直接传入函数和参数

Thread::start除了接收Runnable对象之外,还可以传入函数和参数

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;
}

例子四:线程局部变量存储

    ThreadLocal类为开发者提供了更为简洁的TLS机制使用方法,TLS机制用来保存这样一些变量:它们在不同的线程里有不同的值,并且各自维护,线程不能访问其他线程中的这些变量。

    在一个线程修改的内存内容,对所有线程都生效。这是一个优点也是一个缺点。说它是优点,线程的数据交换变得非常快捷。说它是缺点,一个线程死掉了,其它线程也性命不保; 多个线程访问共享数据,需要昂贵的同步开销,也容易造成同步相关的BUG。

   如果需要在一个线程内部的各个函数调用都能访问、但其它线程不能访问的变量(被称为static memory local to a thread 线程局部静态变量),就需要新的机制来实现。这就是TLS

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;
}

发布了17 篇原创文章 · 获赞 22 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/u013255206/article/details/64130037