H
H
hidden_pingvin2018-05-20 15:01:35
Multithreading
hidden_pingvin, 2018-05-20 15:01:35

How to use one object in different threads?

I have little experience in writing programs in C ++, but in the development of multi-threaded applications I have no experience at all. Tell me how to synchronize an instance of a class in my case?
I'm trying to use mutexes, but I'm doing something wrong, if you look under the debugger, I get different objects in different threads. I need to use field values ​​of the same object in two threads.
The essence of the task: I am making a log file parser, the first thread separates the main events from each other, the second one parses these events in more detail.
main.cpp

mutex m;

void ReadLog(Parser input, mutex &m) {
  if (input.filename != "")
    input.ReadLog(m);
}

void ParceEvents(Parser input, mutex &m) {
  if (input.filename != "")
    input.ParseEvent( m);	
}

int main(int argc, char* argv[]) 
{
  setlocale(LC_ALL, "rus");
  filename = "default_trace.log";

  Parser item = Parser(filename);
  thread thread_1(ReadLog, ref(item), ref(m));
  thread thread_2(ParceEvents, ref(item), ref(m));	
  thread_1.join();
  thread_2.join();
  
  system("pause");
  return 0;
}

Parser.cpp
using namespace std;

Parser::Parser(string input)
{
  this->filename = input;
  this->cursor = 0;
  this->events_parsed = 0;
  this->isReading = false;
}

void Parser::ParseEvent(mutex &m) {
  this->events_parsed = 0;
  this->isReading = true;
  this_thread::sleep_for(chrono::milliseconds(100));
  while (true) {
    cout << "ololo\r\n";
    if (this->events_parsed >= this->cursor) {
      this_thread::sleep_for(chrono::milliseconds(100));
      m.lock();
      this->events_parsed = this->cursor;
      m.unlock();
    }
    else {
      for (int i = this->cursor - this->events_parsed;
        i <= this->cursor; this->events_parsed++) {
        //парсим событие далее

      }
    }
    if (!this->isReading)
      break;
  }
}

//Читает лог-файл
void Parser::ReadLog(mutex &m) {
  regex regex_timestamp(R_DATE);
  
  vector<string> lines_of_event;
  bool read_event = false;

  Event ev;
  ifstream file(this->filename);
  this->isReading = true;
  int i = 0;
  for (string line; getline(file, line); i++) {
    smatch match;

    if (regex_search(line, match, regex_timestamp)) {			
      ev.Lines = lines_of_event;
      this->Events.push_back(ev);
      list<string> words = GetListWords(line);
      vector<string> str_array = GetStringVectorFromList(words);
      ev = Event(str_array);
      lines_of_event.clear();
    }
    else {
      bool line_is_slash_t = (line[0] == '\t' && line.size() == 1);
      bool line_is_empty = (line == "" && line.size() == 0);
      if (!(line_is_slash_t xor line_is_empty)) {
        /*
        regex regex_statement(R_STATEMENT_N);
        if(regex_search(line, match, regex_statement)) {
          //cout << line;
        }
        */
        lines_of_event.push_back(line);
      }
    }
    m.lock();
    this->cursor = i;
    m.unlock();
  }
  cout << "Reading is done...\r\n";
  file.close();
  this->isReading = false;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-05-20
@hidden_pingvin

void ReadLog(Parser& input, mutex &m) {
  if (input.filename != "")
    input.ReadLog(m);
}

void ParceEvents(Parser& input, mutex &m) {
  if (input.filename != "")
    input.ParseEvent( m);	
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question