Anonymous - 2001-04-06

Hello:

I get segmentation fault when I delete thread.
There is my code.
I test a producer consumer model.
lthread is a producer.
testthread is the consumer.
When I press 'x' to exit,I get a Segmentation fault.
Please give me some advice.

I use the Linux Red Hat 6.2.

Thanks.

#include <stdio.h>
#include "thread.h"
#include "tthread.h"

#include "thread.h"

class TThread : public Thread
{
protected:
    bool bExit;
    void setTerminate(void)
    {
        bExit=true;
    }
    bool getTerminate(void)
    {
        return bExit;
    }
public:
    TThread(bool flag) : Thread(flag),bExit(false){}
    TThread(Semaphore *start = NULL, int pri = 0, size_t stack = 0) : Thread(start,pri,stack),bExit(false){}
    TThread(const Thread &th) : Thread(th),bExit(false){}
    ~TThread()
    {
        setTerminate();
        Terminate();
    }
};

FixedBuffer TestPool(10000000,sizeof(int));

class testthread : public TThread
{
protected:
  void Run(void)
  {
    setCancel(THREAD_CANCEL_DISABLED);
    while (1)
    {
      int iBuffer;
      // get job
      TestPool.Wait(&iBuffer);
      // consume job

      // check exit
      if (getTerminate())
        return;
      //Sleep(100);
      Yield();
    }
  }
  void Final()
  {
      Sleep(100);
  }
public:
  testthread(){}
  ~testthread()
  {
    setTerminate();
    Terminate();
  }
};

class lthread : public TThread
{
protected:
  void Run(void)
  {
    setCancel(THREAD_CANCEL_DISABLED);
    while (1)
    {
      int iBuffer;
      // produce some job
      TestPool.Post(&iBuffer);
      if (getTerminate())
        return;
      Sleep(500);
      Yield();
    }
  }
  void Final()
  {
      Sleep(1000);
  }
public:
  lthread(){}
  ~lthread()
  {
    setTerminate();
    Terminate();
  }
};

int main(int argc,char*argv[])
{
    testthread *tt;
    lthread *t2;
    tt=new testthread;
    t2=new lthread;
    tt->Start();
    t2->Start();

    while(getchar()!='x');
    delete tt;
    delete t2;
    return 0;
}