[LC++]Question on pthreads

Jan Pfeifer pfjan at yahoo.com.br
Thu Jul 15 21:34:01 UTC 2004


I think the easiest way would be to read user input from a separate 
thread, and let the "main" thread only wait for the others. If your 
polling thread finishes, the "main" thread could kill/cancel the thread 
reading user input.

I was also wondering if getchar() is interrupted by signals, returning 
"EINTR" ... If it is your exiting thread could signal the "main" thread. 
Use something like SIGUSR and create some kind of flag to check for 
that. If getchar() is not interrupted, try using read(2) directly.

Let me code the idea:
* Disclaimer: havent' tested the code, nor considered race conditions, 
such as the thread endind before the call to the function. *

bool ops = false;

void sigusr_handler( int i )
{ ops = true; }

char our_read_char()
{
      char c;
      int ret;
      while (true) {
        ret = read( 0, &c, 1 );
        if ( ret == 1 ) break;
        if ( ret == -1 && errno = EINTR && ops )
          {
             ops = false;
             throw thread_ended_exception();
          }
        if ( ret == -1 && errno != EINTR )
             throw io_error_exception();
      }
    return c;
}

best luck :)

jan


Krishna Monian wrote:

>I still have a problem though. After creating the
>thread in my main function, this is part of the code
>that follows.
>
>while(getchar() != '\n') {}
>
>So basically I am waiting till the user hits the enter
>key. As soon as the user does this, my program will
>cleanup and quit. The thread that I spawn also does
>some sort of polling operation.
>
>If I use pthread_join in main() then the program
>doesn't terminate when the user hits return (obviously
>it won't, since it has joined the other thread).
>
>How do I go about achieving this?
>
>Thanks
>Krishna
>
>
>--- Jack Lloyd <lloyd at acm.jhu.edu> wrote:
>  
>
>>Yes. If the function created by pthread_create
>>returns a value, then when you
>>call pthread_join, it will return this status to
>>you. In this way you can
>>easily exit the thread and return a status code
>>signaling the rest of your
>>program to abend. That's why your thread start
>>routines must return a void*,
>>that's the status code.
>>
>>If you want to return an integer or some such,
>>remember to dynamically allocate
>>it and return the pointer -- if you return
>>&status_code, it will go away once
>>the thread exits. Obvious, but sometimes it's easy
>>to forget (especially with
>>pthreads). And, of course, free it in your main
>>thread after you pthread_join
>>the other thread.
>>
>>Since this when to the C++ list and not the C list,
>>I'll also remind you that
>>you can't (safely) throw an exception in a thread
>>and have it be caught outside
>>that thread. It could be done, but nobody requires
>>it, and I'm sure it would be
>>very difficult to implement, so nobody does. Just in
>>case you were thinking
>>about doing that to signal errors. :)
>>
>>-Jack
>>
>>On Wed, Jul 14, 2004 at 02:11:06PM -0700, Krishna
>>Monian wrote:
>>    
>>
>>>Hi, 
>>>I have a pthread that is spawned from my main
>>>      
>>>
>>function
>>    
>>
>>>and then the main function just waits until the
>>>      
>>>
>>user
>>    
>>
>>>hits enter. Now I want the program to terminate if
>>>      
>>>
>>a
>>    
>>
>>>something unexpected in the thread spawned. How
>>>exactly do I go about doing this?
>>>
>>>Even better would be if the thread could return a
>>>value to the main function. Is this possible?
>>>
>>>Thanks
>>>Krishna
>>>
>>>
>>>		
>>>__________________________________
>>>Do you Yahoo!?
>>>New and Improved Yahoo! Mail - Send 10MB messages!
>>>http://promotions.yahoo.com/new_mail 
>>>_______________________________________________
>>>This is the Linux C++ Programming List
>>>:
>>>      
>>>
>>http://lists.linux.org.au/listinfo/tuxcpprogramming
>>List
>>_______________________________________________
>>This is the Linux C++ Programming List
>>:
>>http://lists.linux.org.au/listinfo/tuxcpprogramming
>>List
>>
>>    
>>
>
>
>
>		
>__________________________________
>Do you Yahoo!?
>New and Improved Yahoo! Mail - Send 10MB messages!
>http://promotions.yahoo.com/new_mail 
>_______________________________________________
>This is the Linux C++ Programming List
>: http://lists.linux.org.au/listinfo/tuxcpprogramming List
>
>  
>




More information about the tuxCPProgramming mailing list