Recent

Author Topic: reading from stdin in a thread  (Read 5071 times)

Martin V

  • Full Member
  • ***
  • Posts: 139
reading from stdin in a thread
« on: July 15, 2011, 12:18:37 am »
I have written an editor which can be used like | more. In Windows, all works fine, but on Unix systems (Linux, MacOSX), it is not allowed to kill a thread from outside. This is necessary if the editor gets closed by the user.

The code in MyThread.Execute is

Code: [Select]
repeat
    FileRead (stdin, BufferArea, 1);
    {process the character which is hold in BufferArea[1]}
until DoEndThread;

DoEndThread is set from outside the thread, because in Unix systems, the Thread is not allowed to get killed from outside without being terminated by itself.

Now the problem is that if no stdin input is available, the thread sleeps in the FileRead procedure and does not reach the "until" condition to terminate the loop.

Is there a read procedure which returns immediately if nothing is to read at the moment? Or is there another solution?

Martin V

  • Full Member
  • ***
  • Posts: 139
Re: reading from stdin in a thread
« Reply #1 on: July 22, 2011, 05:25:35 pm »
I found now a solution by myself:

If I use AssignFile, reset, read and use the file type "text", I can use the eof function which returns true if no stdin input is available at the moment. In this case, I set the thread sleep for some milliseconds by using "delay".

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: reading from stdin in a thread
« Reply #2 on: July 22, 2011, 09:21:35 pm »
I think a more UNIX-y way would be to use FileRead functions and create a timeout signal handler for the thread. But I'm not sure

 

TinyPortal © 2005-2018