faq.qbk 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. [section:faq Frequently Asked Questions]
  2. [section:dead_lock Why does this produce a deadlock?]
  3. Now let's revisit our c++filt example and we will put in an obvious mistake.
  4. This might however be not as obvious for more complex applications.
  5. ```
  6. vector<string> demangle(vector<string> in)
  7. {
  8. ipstream is;
  9. opstream os;
  10. child c("c++filt", std_out > is, std_in < os);
  11. vector<string> data;
  12. for (auto & elem : data)
  13. {
  14. string line;
  15. getline(is, line);
  16. os << elem;
  17. }
  18. }
  19. ```
  20. We switched the read and write operation up, so that's causing a dead-lock.
  21. This locks immediately. This is because `c++filt` expects input, before
  22. outputting any data. The launching process on the other hand waits for its output.
  23. [endsect]
  24. [section:closep Why does the pipe not close?]
  25. Now for another example, which might look correct, let's consider you want
  26. to use `ls` to read the current directory.
  27. ```
  28. ipstream is;
  29. child c("ls", std_out > is);
  30. std::string file;
  31. while (is >> file)
  32. cout << "File: " << file << endl;
  33. ```
  34. This will also deadlock, because the pipe does not close when the subprocess exits.
  35. So the `ipstream` will still look for data even though the process has ended.
  36. [note It is not possible to use automatically pipe-closing in this library, because
  37. a pipe might be a file-handle (as for async pipes on windows).]
  38. But, since pipes are buffered, you might get incomplete data if you do this:
  39. ```
  40. ipstream is;
  41. child c("ls", std_out > is);
  42. std::string file;
  43. while (c.running())
  44. {
  45. is >> file;
  46. cout << "File: " << file << endl;
  47. }
  48. ```
  49. It is therefore highly recommended that you use the asynchronous api if you are
  50. not absolutely sure how the output will look.
  51. [endsect]
  52. [section:wchar_t When will the codecvt be used?]
  53. Since windows does not use UTF-8 it is sometimes unavoidable to use the `wchar_t` version of the WinApi.
  54. To keep this library consistent it provides `wchar_t` support on posix also.
  55. Since the posix api is purely `char` every `wchar_t` based type will be converted into `char`.
  56. Windows on the other hand is more selective; the default is to use `char`,
  57. but if any parameter requires `wchar_t`, everything will be converted to `wchar_t`.
  58. This also includes `boost::filesystem::path`. Additionally, if the system does not provide
  59. the `char` api (as is the case with Windows CE) everything will also be converted.
  60. [endsect]
  61. [endsect]