⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 punit.cxx

📁 PTypes (C++ Portable Types Library) is a simple alternative to the STL that includes multithreading
💻 CXX
字号:
/* * *  C++ Portable Types Library (PTypes) *  Version 1.7.5   Released 9-Mar-2003 * *  Copyright (c) 2001, 2002, 2003 Hovik Melikyan * *  http://www.melikyan.com/ptypes/ *  http://ptypes.sourceforge.net/ * */#include "ptypes.h"#include "pasync.h"#include "pstreams.h"#ifdef WIN32#  include <windows.h>#else#  include <unistd.h>#endifPTYPES_BEGIN//// internal thread class for running units asynchronously//class unit_thread: public thread{protected:    unit* target;    virtual void execute();public:    unit_thread(unit* itarget);    virtual ~unit_thread();};unit_thread::unit_thread(unit* itarget)    : thread(false), target(itarget){    start();}unit_thread::~unit_thread(){    waitfor();}void unit_thread::execute(){    target->do_main();}//// unit class//unit::unit()    : component(), pipe_next(nil), main_thread(nil),       running(0), uin(&pin), uout(&pout){}unit::~unit(){    delete tpexchange<unit_thread>(&main_thread, nil);}int unit::classid(){    return CLASS_UNIT;}void unit::main(){}void unit::cleanup(){}void unit::do_main(){    try    {        if (!uout->get_active())            uout->open();        if (!uin->get_active())            uin->open();        main();        if (uout->get_active())            uout->flush();    }    catch(exceptobj* e)    {        perr.putf("Error: %s\n", pconst(e->get_message()));        delete e;    }    try    {        cleanup();    }    catch(exceptobj* e)    {        perr.putf("Error: %s\n", pconst(e->get_message()));        delete e;    }    if (pipe_next != nil)        uout->close();}void unit::connect(unit* next){    waitfor();    pipe_next = next;    infile* in = new infile();    outfile* out = new outfile();    next->uin = in;    uout = out;    in->pipe(*out);}void unit::waitfor(){    if (running == 0)        return;    delete tpexchange<unit_thread>(&main_thread, nil);    unit* next = tpexchange<unit>(&pipe_next, nil);    if (next != nil)    {        next->waitfor();        next->uin = &pin;    }    uout = &pout;    running = 0;}void unit::run(bool async){    if (pexchange(&running, 1) != 0)        return;    if (main_thread != nil)        fatal(CRIT_FIRST + 60, "Unit already running");        if (pipe_next != nil)        pipe_next->run(true);    if (async)        main_thread = new unit_thread(this);    else    {        do_main();        waitfor();    }}PTYPES_END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -