[osg-users] SingleThreaded leading to whole application just running on one core

Robert Osfield robert.osfield at gmail.com
Mon Sep 26 11:42:13 PDT 2016

When reviewing doing a code review of the affinity code in OpenThreads
today I noticed that OpenThreads doesn't fully expose all the
functionality available for setting or resetting the thread affinity,
as OpenThrreads just allowed you to specify a single cpu number for a
thread to have affinity with.

To improve the flexibility of setting the Thread processor affinity I
have introduced a new function:

 *  Set the processor affinity mask of current thread.  If you want to
allow thread to run on any processor core use ~0ul for the cpumask
SetProcessorAffinityMaskOfCurrentThread(unsigned long cpumask);

And a new Thread method:

      * If you want this threadd to run on any processor core then use
a cpumask of ~0ul
      * This call must be made before
      * start() or startThread() and has no effect after the thread
      * has been running. Returns 0 on success, implementation's
      * error on failure, or -1 if ignored.
    int setProcessorAffinityMask( unsigned long cpumask);

I have implemented these for pthreads so far, creating a git branch


It's experimental, the API isn't settled yet, but it's a start.  I
will need to boot into Windows tomorrow and implement the Windows
side.  Members of the community are welcome to dive in and beat me to

As a test of this new API I have modified the thread testing example
that Christian wrote and I extended earlier today, the addition simple

        if (arguments.read("--reset"))

What this does is set the affinity of the main thread to run on all
threads.  With this change even the troublesome C++11 threads now work
fine even if the OSG viewer sets the affinity initialize to CPU 0.

This addresses a serious short coming in C++11 threads without needing
to just move all code across to OpenThreads, this call above is
sufficient to fix the lack of affinity support in C++ threads.  Since
OSG applications that might use C++11 threads will be using
OpenThreads anyway this doesn't add any extra dependencies.

This could be used more widely as well, for instance is you want to
set the affinity to just four of the CPU cores this can easily be done
by just setting up the mask appropriately.

I'm tempted to tweak the previous affinity API to:

     OpenThreads::SetProcessorAffinityOfCurrentThread(unsigned int
basecpunum, unsgined int numcores=1)

Which would allow you to say set the affinity for cores 4,5,6,7 via:


I'll sleep on this bit of the API.

These changes in some form will make it into OSG-3.6.  They also could
potentially utilized in osgViewer.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 2285 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20160926/f5ac604f/attachment-0003.cpp>

More information about the osg-users mailing list