Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <pthread.h>
- using namespace std;
- struct thread_s {
- string str;
- string filename;
- pthread_t thread;
- } first, second;
- void *writeToFile(void *threadarg)
- {
- thread_s *structure = (thread_s *)threadarg;
- ofstream file;
- file.open((structure->filename).c_str(), ios::app);
- file << structure->str << endl;
- file.close();
- }
- int main(int argc, char const *argv[])
- {
- string str;
- ifstream file ("lab4-2.txt");
- first.filename = "first.txt";
- second.filename = "second.txt";
- bool odd = true;
- if (file.is_open())
- {
- while (getline (file, str))
- {
- if (odd)
- {
- first.str = str;
- pthread_create(&first.thread, NULL, writeToFile, (void *)&first);
- }
- else
- {
- second.str = str;
- pthread_create(&second.thread, NULL, writeToFile, (void *)&second);
- pthread_join(first.thread, NULL);
- pthread_join(second.thread, NULL);
- }
- odd = !odd;
- }
- pthread_join(first.thread, NULL);
- file.close();
- }
- else
- cout << "Невозможно открыть файл!" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement