eidheim / tiny-process-library

A small platform independent library making it simple to create and stop new processes in C++, as well as writing to stdin and reading from stdout and stderr of a new process
MIT License
338 stars 73 forks source link

implement is_running() #27

Closed hendrikmuhs closed 6 years ago

hendrikmuhs commented 6 years ago

Implements a is_running() method to check whether a process is running. For some usecases this makes the observer thread unnecessary.

For waitpid(unix) I had to cache the exit code, IMHO now it behave also more consistent as get_exit_status can be called multiple times. For windows it should work without this trick.

Windows implementation should work but needs to be tested.

eidheim commented 6 years ago

Great, thank you. A couple of thoughts before I study the changes more:

hendrikmuhs commented 6 years ago

For a try_get_exit_status() (or a parameter blocking=true/false for get_exit_status())

we would need a magic value as return value as I need to distinguish between an exit code and the 'still running' condition. As exit codes can be anything we would need to take a uncommon value and hope no one is using it.

foonathan commented 6 years ago

Or you can use something like std::optional. But given that it is only in std since C++17 it is probably not a good idea to have an external dependency just for that.

eidheim commented 6 years ago

I think we should keep this a c++11 library a few more years, but I agree that std::optional should be used sometime in the future.

Presently, I guess the best solution would be a declaration like this: bool try_get_exit_status(exit_status_type &exit_status) const noexcept. Note also that I added const since I think at least initially that the exit_status should not be stored in a class variable.

An alternative declaration is: std::pair<bool, exit_status_type> try_get_exit_status() const noexcept, but since the returned exit_status_type value is not always valid, I think the this is a less good alternative.

eidheim commented 6 years ago

Also, one of the reasons I prefer try_get_exit_status() instead of is_running() is that try_get_exit_status() is more atomic, and one might possibly take advantage of this, than running two functions sequentially like if(process.is_running()) exit_status = process.get_exit_status();. This is better I think: if(process.try_get_exit_status(exit_status)) // do something

hendrikmuhs commented 6 years ago

Let's keep it simple! An out parameter will do.

For me, is_running() made sense in my application, but try_get_exit_status(...) will do the same job.

Ok, I will change it accordingly.

eidheim commented 6 years ago

Oh, I see the current get_exit_status returns int. Can probably still use int instead of exit_status_type that I mentioned above. Just make sure to static_cast in the windows specific code after receiving the value from the windows API.

edit: fixed ``s

eidheim commented 6 years ago

And the function cannot be const I see now, since one might close file descriptors.

eidheim commented 6 years ago

Thank you again, the commits have been pushed to master. I did some minor changes here: https://github.com/eidheim/tiny-process-library/commit/2984e8d72e07817291c4db8fd1b04e7739219af5. I'll test it on Windows during the weekend I hope.