Hi Community

my client found interesting behaviour on OpenThreads under 64bit OpenSuSE
linux. It seam none of the calls like setSchedulePolicy,
setProcessorAffinity and setSechedulePriority of OpenThreads::Thread are
working. After running a code, they all return 0 (success) but after
inspecting the threads in some tools, they show they are not set as
expected.

So I gave it a try on Ubuntu 64bit. On my linux box none werle working -
all returned -1 (failure). Attached is simple code from Rui's book that
ilustrate the problem.

Any help is appreciated ! Also, the OSG version tested is 3.3.1

Nick
/* -*-c++-*- Copyright (C) 2010 Wang Rui <wangray84 at gmail dot com>
 * OpenSceneGraph Beginners Guide
 * Using a separate thread for supplying data to a node in the main process
*/

#include <osg/Geode>
#include <osgDB/ReadFile>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <iostream>

class DataReceiverThread : public OpenThreads::Thread
{
public:
    DataReceiverThread()
	: OpenThreads::Thread()
    {
	int status = setProcessorAffinity(0);
        printf("setProcessorAffinity status: %d\n", status);
    }
    virtual int cancel()
    {
        _done = true;
        while( isRunning() ) YieldCurrentThread();
        return 0;
    }
    
    virtual void run()
    {
        _done = false;
        _dirty = true;
        do
        {
            YieldCurrentThread();
            
            char ch;
            std::cin.get(ch);
            printf("[%c][%d] ", ch, ch);
            
            switch (ch)
            {
            case 0: break;  // We donĄŻt want ĄŽ\0ĄŻ to be added
            case 9: _done = true; break;  // Tab = 9
            default: addToContent(ch); break;
            }
        } while( !_done );
    }
    
    void addToContent( int ch )
    {
        OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
        _content += ch;
        _dirty = true;
    }
    
    bool getContent( std::string& str )
    {
        OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
        if ( _dirty )
        {
            str += _content;
            _dirty = false;
            return true;
        }
        return false;
    }
    
protected:
    OpenThreads::Mutex _mutex;
    std::string _content;
    bool _done;
    bool _dirty;
};

class UpdateTextCallback : public osg::Drawable::UpdateCallback
{
  
public:
  UpdateTextCallback(DataReceiverThread* rcvrthrd) : rcvrthread(rcvrthrd)
  {}

  virtual void update( osg::NodeVisitor* nv, osg::Drawable* drawable )
    {
        osgText::Text* text = static_cast<osgText::Text*>( drawable );
        if ( text )
        {
            std::string str("# ");
//            if ( DataReceiverThread::instance()->getContent(str) )
            if ( rcvrthread->getContent(str) )
                text->setText( str );
        }
    }
   DataReceiverThread* rcvrthread; 
};

int main( int argc, char** argv )
{
    int status;
	
    osg::ref_ptr<osgText::Text> text = new osgText::Text;
    text->setFont( "fonts/arial.ttf" );
    text->setAxisAlignment( osgText::TextBase::SCREEN );
    text->setDataVariance( osg::Object::DYNAMIC );
    text->setInitialBound( osg::BoundingBox(osg::Vec3(), osg::Vec3(400.0f, 20.0f, 20.0f)) );
	DataReceiverThread* rcvrthread = new DataReceiverThread;
	rcvrthread->Init();
    text->setUpdateCallback( new UpdateTextCallback(rcvrthread));
    
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable( text.get() );
    geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
    
    osgViewer::Viewer viewer;
    viewer.setSceneData( geode.get() );
    viewer.setUpViewInWindow( 50, 50, 640, 480 );
    //status = rcvrthread->setProcessorAffinity(0);
    //printf("setProcessorAffinity status: %d\n", status);

	status = rcvrthread->setSchedulePolicy(OpenThreads::Thread::THREAD_SCHEDULE_ROUND_ROBIN);
	printf("setSchedulePolicy status: %d\n", status);
	if(!status)
	  printf("getSchedulePolicy: %d\n",rcvrthread->getSchedulePolicy()); 
    status = rcvrthread->setSchedulePriority(OpenThreads::Thread::THREAD_PRIORITY_HIGH);
	printf("setSchedulePriority status: %d\n", status);
	if(!status)
	  printf("getSchedulePriority: %d\n",rcvrthread->getSchedulePriority()); 
    rcvrthread->startThread();
    viewer.run();
    rcvrthread->cancel();
    return 0;
}
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to