Thread: How to terminate thread

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    How to terminate thread

    I have multithreaded programming. And my worker thread is a ‘while(1)’ and there is no mechanism that makes it come out of this loop.

    So what is the best way to terminate the thread when application finishes execution?

    Thanks

  2. #2
    Registered User PotitKing's Avatar
    Join Date
    Dec 2001
    Posts
    28

    terminating

    well. I use a loop that is dependent on a flag like:
    while(!exit_flag) {}

    and in main i set the exit flag to true, when i want the thread to quit.
    % gcc -v
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 3.3.3 [FreeBSD] 20031106

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Thanks,

    I am using WIndows NT operating system.

    >And anyway, if your program terminates, all threads will terminate as well.
    But I am not sure of this..

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    27
    not sure if this is what your trying to do
    but If you just want to exit your thread at some time try
    Code:
    pthread_exit(NULL);
    this will exit the thread but not the rest of the program.
    The NULL is used instead of passing in some variable that could store the threads exit info, I think.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    > while(!exit_flag) {}
    > while(1) {}

    Is a good solution, how else are you going to keep the thread alive. Directly a thread exits the thread function its going to die!

    A thread will be killed off when the main thread dies.

    if you want it to die in the while(1) either use break; or return; or use a condition such as !exit_flag.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    So, if I use while(1) like in the following example that wrong is it?
    Code:
    unsigned int WINAPI ComPortThread(LPVOID lpvThreadParam)
    {
    	char szBuffer[10];
    	hComPortHandle = CreateFile("COM1", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    
    	while(1)
    	{
    		ReadFile(hComPortHandle, &szBuffer, 10, (unsigned long*)&iNoOfBytesRead, NULL); 
    		SendMessage(hwnd, COM_MESSAGE, NULL, (LPARAM)&szBuffer);
    	}
    	return TRUE;
    }
    Last edited by Scarlet7; 05-05-2003 at 03:23 PM.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    I want achieve following in worker thread

    If buffer content is not NULL, then send that buffer content over serial line or if time elapsed since last string sent over serial line is more than 5 seconds, then send one default string on serial line.

    I know, I can use WaitForSingleObject, that takes handle and waitTime as parameters. But how will this API treat string as HANDLE, so that I can use this API as????

    WaitForSingleObject( HANDLE to string, 5 seconds)

    Please not that buffer is shared between, main thread and worker thread and is just character array.

    Currently, my worker thread logic(pseudocode) is

    Code:
    Infinite loop
    {
          while(1)
          {
            if ( buffer is not empty )  // just read so no guarding
    	 Acquire mutex
    	 Sendbuffer = buffer
    	 Release Mutex
                    break;
         
          else if ( 5 seconds elapsed)  // send default command
    	 Sendbuffer = “default command”
                    break;
          }
    	
     Log into the file
    
     Send the command on serial line
    
     receive the response from serial line
    
     Log to the file
    
    }
    I am using time function to meaure the time.

    Current design involves busy wait If I can treat string(buffer) as HANDLE, I can make use of WaitForSingleObject function.

    Any comment on how can I make strring as HANDLE.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. Shared memory implementation using thread
    By kumars in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 04:24 AM
  3. user thread library
    By Eran in forum C Programming
    Replies: 4
    Last Post: 06-17-2008, 01:44 AM
  4. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  5. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM