You cannot invoke GUI output operations, such as writing to pcl::Console, from a running thread. However, in PCL 2.0 a
pcl::Thread has off-line console output capabilities, which you can use to implement your Python console output quite easily.
The key member function is:
String pcl::Thread::ConsoleOutputText() constThe basic idea is: Instead of redirecting Python's sys.stdout to pcl::Console, define a pcl::Thread descendant and perform all of your console text output from a running instance of this class. For example (just an outline):
#include <pcl/Atomic.h>
#include <pcl/AutoLock.h>
#include <pcl/Console.h>
#include <pcl/Thread.h>
namespace pcl
{
class ThreadSafeConsoleOutput : public Thread
{
public:
ThreadSafeConsoleOutput() : Thread(), console(), mutex(), scheduled(), finished( 0 )
{
}
virtual void Run()
{
for ( (void)finished.FetchAndStore( 0 ); !finished; )
{
// Keep the UI responsive
Module->ProcessEvents();
// If there is some text scheduled for output, flush it.
// Use Mutex::TryLock() to reduce thread contention.
if ( mutex.TryLock() )
{
if ( !scheduled.IsEmpty() )
{
console.Write( scheduled );
scheduled.Clear();
}
mutex.Unlock();
}
}
}
void Write( const String& s )
{
volatile AutoLock lock( mutex );
scheduled.Append( s );
}
void WriteLn( const String& s )
{
volatile AutoLock lock( mutex );
scheduled.Append( s );
scheduled.Append( '\n' );
}
void WriteLn()
{
volatile AutoLock lock( mutex );
scheduled.Append( '\n' );
}
friend ThreadSafeConsoleOutput& operator <<( ThreadSafeConsoleOutput& o, const String& s )
{
o.Write( s );
return o;
}
void Finish()
{
(void)finished.FetchAndStore( 1 );
}
String Text() const
{
volatile AutoLock lock( mutex );
return ConsoleOutputText();
}
void Flush()
{
volatile AutoLock lock( mutex );
FlushConsoleOutputText();
}
private:
Console console;
Mutex mutex;
String scheduled;
AtomicInt finished;
};
} // pcl
As you see, the ThreadSafeConsoleOutput class above mimics the relevant functionality of pcl::Console, so it can be used to redirect the output of any text-based asynchronous system, such as Python's sys.stdout. Basically, ThreadSafeConsoleOutput allows you to gather console output text in a thread-safe way. At any moment, i.e. while the thread is running, you can call its Text() or Flush() member functions to retrieve its accumulated text or send it to the console, respectively. For example, you can use an external object driven by a pcl::Timer to flush console output at regular intervals.
Let me know if this helps you.
(P.S.: Do you see now why PCL threads cannot be implemented as OpenMP threads? With the current PCL 2.0 Thread class, we have a lot of rich functionality and control that we can only have with our own implementation).