Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Presentation

  • Scope
    • Threading in the different programming languages
    • ACS Managed Threading in C++
  • Duration: 15 minutes

Hands-On Exercise (C++ Only)

Thread Class Definition

Code Block
languagecpp
firstline1
titleacsThreadTest.h
linenumberstrue
collapsetrue
#ifndef _ACS_THREAD_TEST_H
#define _ACS_THREAD_TEST_H

#include "acsThread.h"

class TestACSThread : public ACS::Thread
{
  public:
    TestACSThread(const ACE_CString& name, 
        const ACS::TimeInterval& responseTime=ThreadBase::defaultResponseTime, 
		const ACS::TimeInterval& sleepTime=ThreadBase::defaultSleepTime, bool del=false
    ) : ACS::Thread(name, responseTime, sleepTime, del) {
	    ACS_TRACE("TestACSThread::TestACSThread");
	    loopCounter_m = 0;
	}

    TestACSThread(const ACE_CString& name, const ACS::TimeInterval& responseTime, 
        const ACS::TimeInterval& sleepTime, bool del, const long _thrFlags
	) : ACS::Thread(name, responseTime, sleepTime, del, _thrFlags) {
	    ACS_TRACE("TestACSThread::TestACSThread");
	    loopCounter_m = 0;
	}

    ~TestACSThread() { 
	    ACS_TRACE("TestACSThread::~TestACSThread"); 
	    terminate(); 
	}

    virtual void runLoop() {
	    if (loopCounter_m==100) {
            exit();
	    }	    
        ACS_LOG(LM_SOURCE_INFO, "TestACSThread::runLoop", (LM_INFO, "%s: runLoop (%d)", getName().c_str(), loopCounter_m));
        ++loopCounter_m;
	}

  protected:
    int loopCounter_m;
};

#endif /* end _ACS_THREAD_TEST_H */

Client Code Using Thread Manager

Code Block
languagecpp
firstline1
title<clientFile>.cpp
linenumberstrue
collapsetrue
#include "acsThreadManager.h"
#include "acsThreadTest.h"


int main(int argc, char *argv[]) {
    LoggingProxy logger_m(0, 0, 31);
    LoggingProxy::init(&logger_m);
    ACS_CHECK_LOGGER;


    //Obtain Thread Manager
    ACS::ThreadManager tm(getNamedLogger("ThrMgrLogger"));


    //Create thread
    TestACSThread* test = tm.create<TestACSThread>("TestThread");


    //Resume execution
    test.resume();


    //Wait reasonable time...
    sleep(10);


    //Release Thread Resources
    tm.destroy(test);


    return 0;
}

Component Code Using Thread Manager

Code Block
languagecpp
firstline1
title<idl>.idl
linenumberstrue
collapsetrue
#ifndef _<idl>_IDL_
#define _<idl>_IDL_

#include <acscomponent.idl>

module <module> {
    interface <interface> : ACS::ACSComponent {
        void resume();
        void pause();
    };
};
#endif

...

Code Block
languagecpp
firstline1
title<classFile>.cpp
linenumberstrue
collapsetrue
//Component code...
#include <<classFile>.h>

<class>::<class>(const ACE_CString& name, maci::ContainerServices* containerServices) : ACSComponentImpl(name, containerServices) {
    ACS_TRACE("<class>::<class>");
}

<class>::~<class>() {
    ACS_TRACE("<class>::~<class>");
}

void <class>::execute() {
    test = getContainerServices()->getThreadManager()->create<TestACSThread>("ThreadTest");
}

void <class>::resume() {
    test.resume();
}

void <class>::pause() {
    test.suspend();
}

void <class>::cleanUp() {
    getContainerServices()->getThreadManager()->destroy(test);
}

/* --------------- [ MACI DLL support functions ] -----------------*/
#include <maciACSComponentDefines.h>
MACI_DLL_SUPPORT_FUNCTIONS(<class>)
/* ----------------------------------------------------------------*/

Discussion

  • Dependency with ACE/TAO spawn
  • Upgrading C++ Threading Technology
  • Managed Threading for other languages