By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,917 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,917 IT Pros & Developers. It's quick & easy.

How to create an "endless" std::stringstream?

P: n/a
I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere. I tried to use a std::stringstream to do the
job. I created the stringstream in main() and pass it as pointer into
a few threads. Those threads have the protential to output debug
message at any time (debug << "debug message" << std::endl;). And
there is a special thread to keep reading from the stringstream
object.

Although, I can read the debug message out once I put it in somewhere
else in my program, but when I reach the end of the stream (no debug
message written between interval), the stream sets its failbit, thus,
no more debug message can be written into the stream.

If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.

Mar 22 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ziyan wrote:
I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere. I tried to use a std::stringstream to do the
job. I created the stringstream in main() and pass it as pointer into
a few threads. Those threads have the protential to output debug
message at any time (debug << "debug message" << std::endl;). And
there is a special thread to keep reading from the stringstream
object.

Although, I can read the debug message out once I put it in somewhere
else in my program, but when I reach the end of the stream (no debug
message written between interval), the stream sets its failbit, thus,
no more debug message can be written into the stream.

If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.
Try a queue. Have the threads add strings to the queue,
and the reader remove strings from the front of the queue.
Mar 23 '07 #2

P: n/a
Larry Smith wrote:
Ziyan wrote:
>I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere. I tried to use a std::stringstream to do the
job. I created the stringstream in main() and pass it as pointer into
a few threads. Those threads have the protential to output debug
message at any time (debug << "debug message" << std::endl;). And
there is a special thread to keep reading from the stringstream
object.

Although, I can read the debug message out once I put it in somewhere
else in my program, but when I reach the end of the stream (no debug
message written between interval), the stream sets its failbit, thus,
no more debug message can be written into the stream.

If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.

Try a queue. Have the threads add strings to the queue,
and the reader remove strings from the front of the queue.
Be sure to protect all access to the queue (read & write)
with a mutex.
Mar 23 '07 #3

P: n/a
Hello,

Ziyan wrote:
I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere.
>
If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.
The architecture of the iostreams library suggests to implement another
kind of stream buffer class to support streaming to destinations
different to files and strings, e.g. network sockets. AFAIR such
implementations have been mentioned here and in
comp.lang.c++.moderated. I think the network stream is a typical
example in books about iostreams. I think the book by Langer and Krefft
on C++ iostreams gives a lot of information, although it might be
dated. A lot of useful information on C++ iostreams has been given by
Dietmar Kühl.

If you look at the implementation of the string streams, which are using
a string buffer class, this should give ideas what to do to implement a
streambuf for network sockets. I think under Linux which probably means
g++ and libstdc++ there is already an implementation of a buffer class
available as an extension using a standard C file handle, which might
be immediately usable for network sockets.

Bernd Strieder

Mar 23 '07 #4

P: n/a
"Ziyan" <Zi*******@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
>I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere. I tried to use a std::stringstream to do the
job. I created the stringstream in main() and pass it as pointer into
a few threads. Those threads have the protential to output debug
message at any time (debug << "debug message" << std::endl;). And
there is a special thread to keep reading from the stringstream
object.

Although, I can read the debug message out once I put it in somewhere
else in my program, but when I reach the end of the stream (no debug
message written between interval), the stream sets its failbit, thus,
no more debug message can be written into the stream.

If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.
I once had a similar situation where I needed to pass information to a
thread. I wound up wrapping a std::queue<std::stringin a class with a
named locked on pushes and pops. Unfortunately, I have since lost that code
or I would post it here. It's not that difficult though.
Mar 23 '07 #5

P: n/a
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:bp*************@newsfe06.lga...
"Ziyan" <Zi*******@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
>>I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere. I tried to use a std::stringstream to do the
job. I created the stringstream in main() and pass it as pointer into
a few threads. Those threads have the protential to output debug
message at any time (debug << "debug message" << std::endl;). And
there is a special thread to keep reading from the stringstream
object.

Although, I can read the debug message out once I put it in somewhere
else in my program, but when I reach the end of the stream (no debug
message written between interval), the stream sets its failbit, thus,
no more debug message can be written into the stream.

If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.

I once had a similar situation where I needed to pass information to a
thread. I wound up wrapping a std::queue<std::stringin a class with a
named locked on pushes and pops. Unfortunately, I have since lost that
code or I would post it here. It's not that difficult though.
Oh you're luckly. I looked a little deeper and found the code I was working
on to test this, never put into production, not fully tested. The following
code is not guaranteed to work correctly or be exactly what you want, and it
may be windows specific which you may need to fix.

#include <string>
#include <queue>
#include <iostream>
#include <vector>
#include <sstream>

#include <conio.h>

#include <windows.h>
#include <process.h>

template<typename T, typename F T StrmConvert( F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >to;
return to;
}

class ThreadQueue
{
public:

unsigned int ThreadID;

ThreadQueue(unsigned int limit): Shutdown( false ), ThreadID( 0 )
{
Limit = limit; // Only saved for copy and assignment constructors
which aren't used cause can't figure out good way

handles[SemaphoreIndex] = ::CreateSemaphore(NULL, // no security
attributes
0, // initial count
limit, // max count
NULL); // anonymous

::InitializeCriticalSection(&lock);
}

~ThreadQueue()
{
::EnterCriticalSection(&lock);
Shutdown = true;
while ( ! MsgQueue.empty() )
{
delete MsgQueue.front();
MsgQueue.pop();
}
::LeaveCriticalSection(&lock);

::CloseHandle(handles[SemaphoreIndex]);
::DeleteCriticalSection(&lock);
}

bool AddTail(std::string* pmessage)
{
if ( Shutdown )
return false;
bool result;
::EnterCriticalSection(&lock);
MsgQueue.push(pmessage);
result = ::ReleaseSemaphore(handles[SemaphoreIndex], 1, NULL) != 0;
if (!result)
{
// caller can use ::GetLastError to determine what went wrong
MsgQueue.pop();
}
::LeaveCriticalSection(&lock);
return result;
}

std::string* RemoveHead( const unsigned long Milliseconds = 0 ) //
Milliseconds = INFINITE for blocking
{
if ( Shutdown )
return NULL;

std::string* result;

switch (::WaitForMultipleObjects(1, handles, FALSE, Milliseconds))
{
case SemaphoreIndex: // semaphore
::EnterCriticalSection(&lock);
result = MsgQueue.front();
MsgQueue.pop();
::LeaveCriticalSection(&lock);
return result;

case WAIT_TIMEOUT:
return NULL;

default:
throw "Unknown WaitForMultipleObjects value";
}
}

// Terminate sets shutdown and flushes queue
void Terminate()
{
::EnterCriticalSection(&lock);
Shutdown = true;
while ( ! MsgQueue.empty() )
{
delete MsgQueue.front();
MsgQueue.pop();
}
::LeaveCriticalSection(&lock);
}

void ShutItDown()
{
::EnterCriticalSection(&lock);
Shutdown = true;
::LeaveCriticalSection(&lock);
}

bool isShutDown()
{
::EnterCriticalSection(&lock);
bool ReturnStatus = Shutdown;
::LeaveCriticalSection(&lock);

return ReturnStatus;
}

protected:

enum {SemaphoreIndex};
HANDLE handles[1];
CRITICAL_SECTION lock;

std::queue< std::string* MsgQueue;
unsigned int Limit; // Need for copy and assignment constructors

volatile bool Shutdown;

private:

// Copy Constructor - Doesn't work correctly
ThreadQueue(const ThreadQueue& Queue): Shutdown( false ), ThreadID( 0 )
{
Limit = Queue.Limit; // Only saved for copy and assignment
constructors

handles[SemaphoreIndex] = ::CreateSemaphore(NULL, // no security
attributes
0, // initial count
Limit, // max count
NULL); // anonymous

::InitializeCriticalSection(&lock);
}

// Assignment Constructor - Doesn't work correctly
ThreadQueue& operator=(const ThreadQueue& Queue)
{
if (this == &Queue)
return *this;

ThreadID = 0;

Shutdown = false;

Limit = Queue.Limit; // Only saved for copy and assignment
constructors

handles[SemaphoreIndex] = ::CreateSemaphore(NULL, // no security
attributes
0, // initial count
Limit, // max count
NULL); // anonymous

::InitializeCriticalSection(&lock);

return *this;
}

};

unsigned __stdcall Connection(void* a)
{
ThreadQueue* Interface = reinterpret_cast< ThreadQueue* >( a );

std::string* Message;
for ( int i = 0; i < 100; ++i )
{
if ( Interface->isShutDown() )
return 0;

Message = new std::string;
*Message = *Message + StrmConvert<std::string>( i );
if ( ! Interface->AddTail( Message ) )
{
// Should do some error here, but what?
}
}

return 0;
}

struct ThreadInfo
{
unsigned int ThreadID;
HANDLE Handle;
};

int ThreadQueuemain()
{
const static int NumOfQueues = 5000;

std::vector< ThreadInfo Threads;
std::vector< ThreadQueue* Interfaces;

std::cout << "Initializing Threads..." << std::endl;
for ( int i = 0; i < NumOfQueues; ++i )
{
std::vector< ThreadQueue* >::iterator TQit = Interfaces.insert(
Interfaces.end(), new ThreadQueue( 32767 ) );
std::vector< ThreadInfo >::iterator TIit = Threads.insert(
Threads.end(), ThreadInfo() );
(*TIit).Handle = reinterpret_cast<HANDLE>(_beginthreadex(0, 0,
Connection, (void*) *TQit, 0, &(*TIit).ThreadID));
}

std::string* Message = NULL;
while ( ! _kbhit() )
{

for ( std::vector<ThreadQueue*>::iterator it = Interfaces.begin();
it != Interfaces.end(); ++it )
{
Message = (*it)->RemoveHead();
if ( Message != NULL )
{
std::cout << *Message << " ";
delete Message;
}
}

}

for ( std::vector<ThreadQueue*>::iterator it = Interfaces.begin(); it !=
Interfaces.end(); ++it )
(*it)->Terminate();

// Wait for all threads to terminate before ending
for ( std::vector< ThreadInfo >::iterator it = Threads.begin(); it !=
Threads.end(); ++it )
{
WaitForSingleObject( (*it).Handle, INFINITE );
CloseHandle( (*it).Handle );
}

for ( std::vector<ThreadQueue*>::iterator it = Interfaces.begin(); it !=
Interfaces.end(); ++it )
delete (*it);

std::cout << "\nPress Return..." << std::flush;

std::string wait;
std::getline( std::cin, wait );

return 0;
}
Mar 23 '07 #6

P: n/a
Thanks guys! I really appreciated it! Thank you!

Mar 24 '07 #7

P: n/a
"Ziyan" <Zi*******@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
Thanks guys! I really appreciated it! Thank you!
Incidently, in the code I provided I was using a std::queue<std::string*>.
My feeling was so I wouldnt' have to copy the string. I think if I did it
today, I would probably go with a std::queue<std::stringwhich would
simplify the use quite a bit. It wouldnt' be hard at all to change the code
to use a std::queue<std::stringinstead of current. It's up to you.
Mar 24 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.