Home Help Center

Threading Issue

dtogiasdtogias Member Dimitris TogiasOrganization: NTUAProject: Building a custom server
I'm trying to use class Thread from the IceUtils.dll and when i create a new thread i'm always getting an unhandled exception. The situation is like this:

class CReaderThread : public IceUtil::Thread
{
void run() { }
}

class CWriterThread : public IceUtil::Thread
{
void Start() {
_ThreadB = new CReaderThread();

this->start();
}

void Stop() {
//Stop _ThreadB
_ThreadB.getThreadControl().join();

//Stop this (class WriterThread)
}

CReaderThreadPtr _ThreadB;
}


The issue is that when i call the CWriterThread Start method for the first time the 2 threads start normally. Then i call the Stop function and everything is OK. But if i call for a second time the Start method of the CWriteThread the i get an exception on the line that creates the _ThreadB = new CReaderThread().
If i comment out the line: this->start();,
then i can call the Start and Stop methods as many times as i want.

Maybe i have misunderstood something in the way that i should be using the IceUtil::Thread class.

DT

Comments

  • bernardbernard Jupiter, FLAdministrators, ZeroC Staff Bernard NormierOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    Hi Dimitris,

    Welcome to our forums.

    Your code fragment is incomplete since you don't show where you start _ThreadB. However, the issue appears to be that you're trying to reuse a Thread object--start a thread with this object, stop the thread, and then restart it. Our Thread class does not support this usage: each object is for "one time" use: if you want to create and start a new thread, you need to create a new Thread object.

    Best regards,
    Bernard
  • dtogiasdtogias Member Dimitris TogiasOrganization: NTUAProject: Building a custom server
    Yes you are right i forgot to include a line. Here is the complete code.

    class CReaderThread : public IceUtil::Thread
    {
    void run() { }
    }

    class CWriterThread : public IceUtil::Thread
    {
    void Start() {
    _ThreadB = new CReaderThread();
    _ThreadB->start();

    this->start();
    }

    void Stop() {
    //Stop _ThreadB
    _ThreadB.getThreadControl().join();

    //Stop this (class WriterThread)
    }

    CReaderThreadPtr _ThreadB;
    }

    The issue is that i'm getting the unhandled exception when i call _ThreadB->start not when i call this->start. That was the reason that i was puzzled.
    Thanks for you reply.

    DT
  • bernardbernard Jupiter, FLAdministrators, ZeroC Staff Bernard NormierOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    dtogias wrote: »
    The issue is that i'm getting the unhandled exception when i call _ThreadB->start not when i call this->start.

    You should enclose your code in a try/catch block to avoid unhandled exceptions. Here, the exception was IceUtil::ThreadStartedException.

    Cheers,
    Bernard
Sign In or Register to comment.