Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <pthread.h>
  5. using namespace std;
  6.  
  7. struct thread_s {
  8. string str;
  9. string filename;
  10. pthread_t thread;
  11. } first, second;
  12.  
  13. void *writeToFile(void *threadarg)
  14. {
  15. thread_s *structure = (thread_s *)threadarg;
  16. ofstream file;
  17. file.open((structure->filename).c_str(), ios::app);
  18. file << structure->str << endl;
  19. file.close();
  20. }
  21.  
  22. int main(int argc, char const *argv[])
  23. {
  24. string str;
  25. ifstream file ("lab4-2.txt");
  26. first.filename = "first.txt";
  27. second.filename = "second.txt";
  28. bool odd = true;
  29. if (file.is_open())
  30. {
  31. while (getline (file, str))
  32. {
  33. if (odd)
  34. {
  35. first.str = str;
  36. pthread_create(&first.thread, NULL, writeToFile, (void *)&first);
  37. }
  38. else
  39. {
  40. second.str = str;
  41. pthread_create(&second.thread, NULL, writeToFile, (void *)&second);
  42. pthread_join(first.thread, NULL);
  43. pthread_join(second.thread, NULL);
  44. }
  45. odd = !odd;
  46. }
  47. pthread_join(first.thread, NULL);
  48. file.close();
  49. }
  50. else
  51. cout << "Невозможно открыть файл!" << endl;
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement