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

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

 例子一: 传入对象

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

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

#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提供了一个适配器来使线程能够从你指定的入口启动,并且无需修改已有的类:

#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对象之外,还可以传入函数和参数

#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

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

例子五:综合性例子

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

    

文章版权声明

 1 原创文章作者:583566785,如若转载,请注明出处: https://www.52hwl.com/34523.html

 2 温馨提示:软件侵权请联系469472785#qq.com(三天内删除相关链接)资源失效请留言反馈

 3 下载提示:如遇蓝奏云无法访问,请修改lanzous(把s修改成x)

 免责声明:本站为个人博客,所有软件信息均来自网络 修改版软件,加群广告提示为修改者自留,非本站信息,注意鉴别

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023年7月15日 下午12:50
下一篇 2023年7月15日 下午12:50