Evo C++ Library v0.5.1
Classes | Namespaces | Macros | Typedefs | Functions
iothread.h File Reference

Evo Input/Output streams with multithreading. More...

#include "io.h"
#include "thread.h"
#include "file.h"

Go to the source code of this file.

Classes

struct  ConsoleMT
 Multithreaded console I/O. More...
 
struct  StreamMT< T >
 Stream with mutex for multithreaded synchronization. More...
 
struct  StreamMT< File >
 Stream with mutex for multithreaded synchronization, specialized for File. More...
 

Namespaces

 evo
 Evo C++ Library namespace.
 

Macros

#define EVO_CATCH_MT(CODE)
 Catch Evo Exception and print thread-safe error message to stderr. More...
 
#define EVO_CONSOLE_MT   static evo::ConsoleMT& c = evo::con_mt()
 Shortcut to define a ConsoleMT for I/O. More...
 
#define EVO_IO_MT(STREAM, CODE)   { Mutex::Lock evo_streamlock_(STREAM .mutex); STREAM.stream CODE; }
 Lock mutex and run stream operation code. More...
 
#define INCL_evo_iothread_h
 

Typedefs

typedef StreamMT< File > FileMT
 File with mutex for synchonization – see File and StreamMT<File> More...
 
typedef StreamMT< PipeIn > PipeInMT
 Input pipe with mutex for synchonization – see StreamMT. More...
 
typedef StreamMT< PipeOut > PipeOutMT
 Output pipe with mutex for synchonization – see StreamMT. More...
 

Functions

ConsoleMT & con_mt ()
 Shortcut for ConsoleMT::get(). More...
 

Detailed Description

Evo Input/Output streams with multithreading.

Macro Definition Documentation

◆ EVO_CATCH_MT

#define EVO_CATCH_MT (   CODE)
Value:
catch (const evo::Exception& e) { \
{ \
evo::ConsoleMT& c = evo::con_mt(); \
Mutex::Lock lock(c.err.mutex); \
c.err.stream << evo::NL << e.msg() << " -- "; \
evo::errormsg_out(c.err.stream, e.error()); \
c.err.stream << evo::NL; \
} \
CODE; \
}
TOut & errormsg_out(TOut &out, Error err)
Write error message with errno to output stream/string.
Definition: sys.h:1203
ConsoleMT & con_mt()
Shortcut for ConsoleMT::get().
Definition: iothread.h:233
Evo base exception class.
Definition: sys.h:1214
static const NewlineDefault & NL
Default newline type.
Definition: sys.h:785
const char * msg() const
Get exception message.
Definition: sys.h:1254
Error error() const
Get error code.
Definition: sys.h:1260

Catch Evo Exception and print thread-safe error message to stderr.

  • This does not terminate the program when the exception is caught, pass abort() (or similar) in CODE to do that
  • Use after a try or catch block, where a "catch" would normally go (see example below)
  • For non-thread-safe variant see EVO_CATCH()
Parameters
CODECode to run if exception is caught, abort() to terminate process, use just a semi-colon for none
Example
#include <evo/iothread.h>
int main() {
try {
// ...
return 0;
} EVO_CATCH_MT(abort())
}

◆ EVO_CONSOLE_MT

#define EVO_CONSOLE_MT   static evo::ConsoleMT& c = evo::con_mt()

Shortcut to define a ConsoleMT for I/O.

  • This defines "static ConsoleMT& c" in current scope
  • This is normally used at the top of each function using ConsoleMT streams
    • This may be used globally (not recommended), but you'll run into "redefinition of 'c'" errors if used (or included) in multiple source files
      • Note: Evo examples often use a static global for simplicity
    • This won't work in a class or struct outside of a function (must initialize static member out of line)
  • For non-thread-safe variant see EVO_CONSOLE
Example
#include <evo/iothread.h>
using namespace evo;
void hello() {
EVO_IO_MT(c.out, << "Hello" << NL)
}
int main() {
EVO_IO_MT(c.out, << "Calling hello()" << NL)
hello();
return 0;
}

◆ EVO_IO_MT

#define EVO_IO_MT (   STREAM,
  CODE 
)    { Mutex::Lock evo_streamlock_(STREAM .mutex); STREAM.stream CODE; }

Lock mutex and run stream operation code.

  • This is a shortcut for making a stream operation thread-safe
  • Note: This operates in a new scope so an ending semi-colon isn't required (though won't hurt)
Parameters
STREAMStreamMT or StreamExtMT object to use
CODECode to use on stream, this code goes right after the string object name, it may start with a dot (.) or operator like <<
Example
#include <evo/iothread.h>
using namespace evo;
static ConsoleMT& c = con_mt();
int main() {
// Thread safe console output
EVO_IO_MT(c.out, << "Hello World " << 123 << NL) // ending semicolon not required
// Thread safe console output without macro
{ Mutex::Lock lock(c.out.mutex); c.out.stream << "Hello World " << 123 << NL; }
}

◆ INCL_evo_iothread_h

#define INCL_evo_iothread_h