Commit bccc15d9 authored by John Fox Woland's avatar John Fox Woland

+ <iostream>

+ Возвращаемое значение из выполненного потока
v Деструктор MainThread
parent 9d16c642
......@@ -68,7 +68,9 @@ void ChildThread::task ()
}
}
if (!error)
if (error)
setReturn(Error::FileRead);
else
{
if (nl)
{
......@@ -131,7 +133,10 @@ void ChildThread::startAction ()
if (m_fd > 0)
setActive();
else
{
setReturn(code);
m_cfg.parent->message(new Error(this, code, errMsg));
}
}
void ChildThread::stopAction ()
......
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "mainthread.h"
......@@ -7,15 +8,13 @@
void usage ()
{
printf
(
"USAGE: thread-messaging {--help | --defaults | <thread config> <thread config> [<thread config> …]}\n"
" <thread config> := <mode>:<delay>:<path>\n"
" <mode> := {r | w}\n"
" <delay> := integer value in ms\n"
" <path> := string containing path to file\n"
"\n"
);
std::cout
<< "USAGE: thread-messaging {--help | --defaults | <thread config> <thread config> [<thread config> …]}" << std::endl
<< " <thread config> := <mode>:<delay>:<path>" << std::endl
<< " <mode> := {r | w}" << std::endl
<< " <delay> := integer value in ms" << std::endl
<< " <path> := string containing path to file" << std::endl
<< std::endl;
}
int addConfig (Thread::Config **cfg, int index, int count, char *cfgString)
......@@ -50,6 +49,7 @@ int addConfig (Thread::Config **cfg, int index, int count, char *cfgString)
return 20;
param = delim + 1;
//FIX: Too simple check. Must be rewritten
for (int i = 0; i < index; i++)
if (strcmp(param, cfg[i]->path) == 0)
return 21;
......@@ -124,27 +124,25 @@ int main (int argc, char **argv)
switch (r)
{
case 20:
printf("[Error] Wrong config format: %s\n", argv[i]);
std::cerr << "[Error] Wrong config format: " << argv[i] << std::endl;
break;
case 21:
printf("[Error] Duplicate path entry in config: %s\n", argv[i]);
std::cerr << "[Error] Duplicate path entry in config: " << argv[i] << std::endl;
break;
case 22:
printf("[Error] Must be at least one read and write thread\n", argv[i]);
std::cerr << "[Error] Must be at least one read and write thread" << std::endl;
break;
}
return r;
}
}
printf("Starting with configuration:");
std::clog << "Starting with configuration:";
for (int i = 1; i < argc; i++)
printf(" %s", argv[i]);
printf("\n");
std::clog << argv[i];
std::clog << std::endl;
MainThread thread(argc - 1, cfg);
thread.message(new Command(NULL, Command::Start));
thread.wait();
return 0;
return thread.wait();
}
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "mainthread.h"
#include "command.h"
......@@ -24,8 +25,13 @@ MainThread::MainThread (int cCount, Thread::Config **cfg)
MainThread::~MainThread ()
{
for (int i = 0; i < m_cCount; i++)
{
delete m_cfg[i];
delete m_children[i];
}
delete[] m_cfg;
delete[] m_children;
}
void MainThread::startAction ()
......@@ -54,13 +60,14 @@ void MainThread::readError (Error *msg)
{
if (msg->code() == Error::Ok)
{
printf("%s\n", msg->message());
std::clog << msg->message() << "\n";
if ((msg->sender()->mode() == Thread::Read) && (!--m_rCount))
stop();
}
else
{
printf("[Error] %s\n", msg->message());
setReturn(msg->code());
std::cerr << "[Error] " << msg->message() << "\n";
stop();
}
}
......
......@@ -8,6 +8,7 @@ Thread::Thread ()
m_active = false;
m_valid = false;
m_first = m_last = NULL;
m_retval = 0;
pthread_mutex_init(&m_stateMutex, NULL);
pthread_mutex_init(&m_msgMutex, NULL);
......@@ -22,12 +23,15 @@ Thread::Thread ()
}
}
void Thread::wait ()
int Thread::wait ()
{
if (!valid())
return;
return -1;
int *res;
pthread_join(m_thread, (void **)&res);
pthread_join(m_thread, NULL);
return *res;
}
void Thread::message (Message *msg)
......@@ -79,6 +83,11 @@ bool Thread::valid ()
return valid;
}
void Thread::setReturn (int retval)
{
m_retval = retval;
}
bool Thread::readMessage ()
{
if (!valid())
......@@ -156,5 +165,5 @@ void Thread::run ()
}
}
pthread_exit(NULL);
pthread_exit(&m_retval);
}
......@@ -30,7 +30,7 @@ class Thread
Thread();
virtual inline Mode mode() { return Invalid; }
void wait();
int wait();
void message(Message *msg);
protected:
......@@ -39,6 +39,7 @@ class Thread
void setActive(bool active = true);
void stop();
bool valid();
void setReturn(int retval);
virtual void beforeMessageProcess(Message *msg) {}
virtual void afterMessageProcess(Message *msg) {}
......@@ -54,6 +55,7 @@ class Thread
pthread_cond_t m_msgCondition;
bool m_valid, m_running, m_active;
Message *m_first, *m_last;
int m_retval;
void run();
bool readMessage();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment