How to perform the work on the server in a separate thread?

RCF support and general discussion.
Post Reply
acDev
Posts: 27
Joined: Tue Oct 08, 2013 3:08 pm
Location: Moscow
Contact:

How to perform the work on the server in a separate thread?

Post by acDev »

The client sends a command to run the executable module that is on the server machine.
Must be in a separate thread to standard output read data in the database.

It seems to be suited for this asynchronous calls. But just disconnect the client cancels the operation.

I must use RCF::ThreadPool.setTask ?

jarl
Posts: 238
Joined: Mon Oct 03, 2011 4:53 am
Contact:

Re: How to perform the work on the server in a separate thre

Post by jarl »

First off, do you really need to run the code on a particular thread? If you are printing information to standard output, you can protect the print calls with a mutex to ensure that they don't interfere with print calls from other threads.

If you do need to execute a remote call on a thread other than the RCF thread that receives the call, you can use asynchronous dispatching, described here:

http://www.deltavsoft.com/doc/rcf_user_ ... ispatching

However, the first option is simpler, if it will work for you.
Kind Regards

Jarl Lindrud
Delta V Software
http://www.deltavsoft.com

acDev
Posts: 27
Joined: Tue Oct 08, 2013 3:08 pm
Location: Moscow
Contact:

Re: How to perform the work on the server in a separate thre

Post by acDev »

jarl wrote:If you do need to execute a remote call on a thread other than the RCF thread that receives the call, you can use asynchronous dispatching, described here:

http://www.deltavsoft.com/doc/rcf_user_ ... ispatching
Imagine this situation:

Code: Select all

class HelloWorldImpl
{
public:

    typedef RCF::RemoteCallContext<int, const std::string&> PrintContext;

    int Print(const std::string & s)
    {
        // Capture current remote call context.
        PrintContext printContext(RCF::getCurrentRcfSession());

        // Create a new thread to dispatch the remote call.
        RCF::ThreadPtr threadPtr( new RCF::Thread( boost::bind(
            &HelloWorldImpl::threadFunc, 
            this, 
            printContext) ) );

        return 0;
    }

    void threadFunc(PrintContext printContext)
    {
        const std::string & s = printContext.parameters().a1.get();
        std::cout << "I_HelloWorld service: " << s << std::endl;
        RCF::sleepMs(200000);     // <===
        printContext.parameters().r.set( s.length() );
        printContext.commit();
    }
};

int main()
{
    ...
    RCF::RcfServer server( RCF::TcpEndpoint("0.0.0.0", 50001) );
    ...
    server.start();
    // remote client call remote method "Print"
    server.stop();   // <=== 
    ...
}
Calling functions "server.stop" as it will affect the additional thread?

acDev
Posts: 27
Joined: Tue Oct 08, 2013 3:08 pm
Location: Moscow
Contact:

Re: How to perform the work on the server in a separate thre

Post by acDev »

Maybe I need to to use "Creating a service"?
http://www.deltavsoft.com/doc/archive/1 ... cture.html

jarl
Posts: 238
Joined: Mon Oct 03, 2011 4:53 am
Contact:

Re: How to perform the work on the server in a separate thre

Post by jarl »

Custom services, as described in your link, are no longer supported in RCF.

If you start your own threads from within an RCF server object, you'll need to manage those threads separately from the RcfServer. So before you call RcfServer::stop(), you'll need to call your own code to stop all the custom threads that have been started.
Kind Regards

Jarl Lindrud
Delta V Software
http://www.deltavsoft.com

acDev
Posts: 27
Joined: Tue Oct 08, 2013 3:08 pm
Location: Moscow
Contact:

Re: How to perform the work on the server in a separate thre

Post by acDev »

jarl wrote:Custom services, as described in your link, are no longer supported in RCF.
Method "addService" maked as public. Implementation took from PingBackService.
jarl wrote:If you start your own threads from within an RCF server object, you'll need to manage those threads separately from the RcfServer. So before you call RcfServer::stop(), you'll need to call your own code to stop all the custom threads that have been started.
Now is the time involved in creation "manage those threads separately from the RcfServer".

jarl
Posts: 238
Joined: Mon Oct 03, 2011 4:53 am
Contact:

Re: How to perform the work on the server in a separate thre

Post by jarl »

Well, doctoring the source code won't make you very popular with our support team :)

However, we'll need to find a way to accomplish what you want to do. Is it possible for you to simply use a multi-threaded server?

http://www.deltavsoft.com/doc/rcf_user_ ... _threading

If not, what is it you need, that a multi-threaded server doesn't provide?
Kind Regards

Jarl Lindrud
Delta V Software
http://www.deltavsoft.com

acDev
Posts: 27
Joined: Tue Oct 08, 2013 3:08 pm
Location: Moscow
Contact:

Re: How to perform the work on the server in a separate thre

Post by acDev »

jarl wrote:Well, doctoring the source code won't make you very popular with our support team :)
I use the "RCF" for home use only. Google is my support team ;)
jarl wrote:However, we'll need to find a way to accomplish what you want to do. Is it possible for you to simply use a multi-threaded server?

http://www.deltavsoft.com/doc/rcf_user_ ... _threading

If not, what is it you need, that a multi-threaded server doesn't provide?
I would like to see in the "RCF" is not only an internal thread pool, as well as "Thread Manager".
That is, to ensure easy generation of "background jobs" that will be executed in individual threads (without thread pool).


Classes boost::thread & boost::thread_groups i do not use (i do not want to compile BOOST).
Now i using RCF::Thread, RCF::Lock, RCF::Condition and etc.

Post Reply