Thread: Thread

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    29

    Angry Thread

    Hello All,

    I am learning how to use the TThread class to create
    another thread from my mainform. Essentially, I am writing
    a file/folder search program that uses a recursive function
    called vSearch_Folder( ....) which takes 4 parameters. I am calling
    this function within the Execute method of the thread object, using the code below:-


    void __fastcall searchThread::Execute()
    {
    FreeOnTerminate = false;

    // call search functions
    mainform->vSearch_Folder(THREAD_FolderPath,
    THREAD_SearchString,
    THREAD_iSwitch,
    pTHREAD_iCounter);

    // free up memory for thread object
    FreeOnTerminate = true;
    }

    I am using a flag, FreeOnTerminate, to determine when the thread
    has finished execution. (ie when the recursive function has finished.)
    THe value of this flag is determined in mainform by calling inline

    bool bGetThreadStatus(void){return FreeOnTerminate;};

    which is declared in the class derived from TThread.



    Q. Is there a better way to determine when a thread finishes execution ?

    Thanks,


    bigSteve

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: Thread

    Originally posted by bigSteve
    Hello All,

    I am learning how to use the TThread class to create
    another thread from my mainform. Essentially, I am writing
    a file/folder search program that uses a recursive function
    called vSearch_Folder( ....) which takes 4 parameters. I am calling
    this function within the Execute method of the thread object, using the code below:-


    void __fastcall searchThread::Execute()
    {
    FreeOnTerminate = false;

    // call search functions
    mainform->vSearch_Folder(THREAD_FolderPath,
    THREAD_SearchString,
    THREAD_iSwitch,
    pTHREAD_iCounter);

    // free up memory for thread object
    FreeOnTerminate = true;
    }

    I am using a flag, FreeOnTerminate, to determine when the thread
    has finished execution. (ie when the recursive function has finished.)
    THe value of this flag is determined in mainform by calling inline

    bool bGetThreadStatus(void){return FreeOnTerminate;};

    which is declared in the class derived from TThread.



    Q. Is there a better way to determine when a thread finishes execution ?

    Thanks,


    bigSteve
    Yes.........unless you mark FreeOnTerminate as a "volatile", there's no guarantee that your code will work. 1 standard unprotected variable accessed by 2 threads is often a recipie for disaster!

    A better way is for you to use one of window's thread sync objects...Events (with CreateEvent()) or Mutexes (CreateMutex()) are good possibilities depending on what you want to do

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  2. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  3. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  4. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  5. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM