🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #1244

Re: Passing info between threads

#1244
From: Paul at NCF
Date: Fri, 23 Feb 2007 08:04
96 lines
3554 bytes
Hi,

I was going to send an example this morning, but looks like Bob has beat me 
to it.  However as a bit more help:

I use a set of classes that I've built up to standardise what I want my 
threads/tasks to do for me and my fellow team members.  It may not be 
perfect, but I've yet to make any changes to it in the last year or so. 
I've built up all the other classes for sockets and serial comms around 
this.

Hope this might help.

Paul

__fastcall CTask::CTask( char *  name_string,
                         ulong   identity,
                         ulong   priority )
    : TThread(true)
{
    // make sure thread is deleted by owner
    FreeOnTerminate = false;

    // initialise member variables
    MyHandlesCount = 0;
    MyHandles    = NULL;
    MyEvent      = NULL;

    // create event and store handle
    // unnamed os object to avoid unnecessary issues!!
    MyEvent = new TEvent ( NULL, false, false, "" );
    MyHandles[MyHandlesCount++] = (HANDLE) MyEvent->Handle;
}

void
CTask::WakeUpSignal( void )
{
    MyEvent->SetEvent();
}

bool
CTask::WaitForWakeUp( int wait_time )
{
    bool result = false;
    ulong apilimit = MyHandlesCount - 1;

    WMOresult = WaitForMultipleObjects( MyHandlesCount, MyHandles, false, 
wait_time );

    // test for API failure code
    if( (WMOresult >= WAIT_OBJECT_0)
     && (WMOresult <= (WAIT_OBJECT_0 + apilimit))
    ){
        result = false;
    }
    else
    if( (WMOresult >= WAIT_ABANDONED_0)
     && (WMOresult <= (WAIT_ABANDONED_0 + apilimit))
    ){
        result = true;
    }
    else
    if (WMOresult == WAIT_TIMEOUT)
    {
        // timeout waiting for event
        result = true;
    }
    else
    {
        int i = GetLastError();
        result = true;
    }
    return result;
}

void Controller::SendMessageToTask( SomeMessageType * message )
{
    // list type hidden from task owner, it doesn't need to know
    if (MyMbx)
    {
        // mailbox signals the task event when adding a message
        MyMbx->AddMailMsg(message);
    }
}

void Controller::Execute()
{
    while (!Terminated)
    {
        // wait for wake up if nothing to process
        if (MyMbx->GetMailMsg() == NULL)
        {
            result = WaitForWakeUp( 100 );
        }
        etc.....
}
Technical Details
Message-ID: <45de9fd4@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!newsfeed.gamma.ru!Gamma.RU!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <45d9e83a$1@newsgroups.borland.com> <h21mt21bg7maqp25d2lqkr9b1iet0eu3g6@4ax.com> <45db2486$1@newsgroups.borland.com> <vj8nt2llf7deda6ioqggci4rds885qcmne@4ax.com> <45dcc0cb@newsgroups.borland.com> <45dd557c$1@newsgroups.borland.com> <45ddf727@newsgroups.borland.com> <45de1d0f@newsgroups.borland.com> <45de262d$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,234a9a04e64155b0
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Paul at NCF" <notplmacca@clara.co.uk>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <45d9e83a$1@newsgroups.borland.com> <h21mt21bg7maqp25d2lqkr9b1iet0eu3g6@4ax.com> <45db2486$1@newsgroups.borland.com> <vj8nt2llf7deda6ioqggci4rds885qcmne@4ax.com> <45dcc0cb@newsgroups.borland.com> <45dd557c$1@newsgroups.borland.com> <45ddf727@newsgroups.borland.com> <45de1d0f@newsgroups.borland.com> <45de262d$1@newsgroups.borland.com>
Subject: Re: Passing info between threads
Date: Fri, 23 Feb 2007 08:04:09 -0000
Lines: 98
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 86.133.183.224
Message-ID: <45de9fd4@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1172217812 86.133.183.224 (23 Feb 2007 00:03:32 -0800)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!newsfeed.gamma.ru!Gamma.RU!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail