[osg-users] SingleThreaded leading to whole application just running on one core
Robert Osfield
robert.osfield at gmail.com
Sun Sep 25 08:16:22 PDT 2016
Thanks for the example Fabian, I'm away from my desktop system this
weekend, will have a look in the week.
Have you tried setting the affinity of the threads that are created?
Have you tried creating the threads before the call to viewer.realize()?
The way things are behaving looks to be down to the way that the Linux
threading is forcing the inheritance of the threading affinity of the
main thread to child threads.
I don't know if there is an setting on the Linux threads side that can
change this behaviour so it's more consistent with other platforms.
If there is a solution this then the place to apply it would the
OpenThreads pthread implementation.
Robert.
On 25 September 2016 at 10:44, Christoph Weiss <weiss at wsoptics.de> wrote:
> I wrote a simple sample program that produces the following output:
>
> % g++ -std=c++14 -losgViewer -pthread a.cpp && time ./a.out
> Thread 5 done
> Thread 9 done
> Thread 4 done
> Thread 0 done
> Thread 6 done
> Thread 15 done
> Thread 12 done
> Thread 13 done
> Thread 11 done
> Thread 7 done
> Thread 8 done
> Thread 1 done
> Thread 3 done
> Thread 10 done
> Thread 14 done
> Thread 2 done
> ./a.out 161.06s user 0.02s system 392% cpu 41.036 total
>
> % g++ -std=c++14 -losgViewer -pthread -DSINGLETHREADED a.cpp && time ./a.out
> Thread 6 done
> Thread 13 done
> Thread 9 done
> Thread 8 done
> Thread 15 done
> Thread 10 done
> Thread 11 done
> Thread 14 done
> Thread 5 done
> Thread 4 done
> Thread 1 done
> Thread 3 done
> Thread 7 done
> Thread 12 done
> Thread 2 done
> Thread 0 done
> ./a.out 121.23s user 0.02s system 99% cpu 2:01.92 total
>
> The program looks like this:
>
> --------
> #include <future>
> #include <iostream>
> #include <vector>
>
> #include <osgViewer/Viewer>
> #include <osgViewer/ViewerBase>
>
> int main() {
> constexpr int numThreads = 16;
> osgViewer::Viewer viewer;
> #ifdef SINGLETHREADED
>
> viewer.setThreadingModel(osgViewer::ViewerBase::ThreadingModel::SingleThreaded);
> #endif
> viewer.realize();
>
> const auto compute = [] (const int i) {
> for(volatile int n = 0; n != 1'000'000'000; ++n) {
> ++n;
> --n;
> }
> std::cout << "Thread " << i << " done\n";
> };
>
> std::vector<std::future<void>> futures;
> for(int i = 0; i != numThreads; ++i) {
> futures.push_back(std::async(std::launch::async, compute,
> i));
> }
> for(auto & future: futures) {
> future.wait();
> }
>
> return 0;
> }
> --------
>
> Christoph
>
> On 09/25/2016 01:42 AM, Fabian Wiesel wrote:
>>
>> int main(int argc, char **argv) {
>> std::vector<int> myvector(1024);
>> osgViewer::Viewer viewer;
>> viewer.setSceneData( node );
>> viewer.setThreadingModel(SingleThreaded);
>> viewer.realize(); // calling ViewerBase::setUpThreading() ->
>> OpenThreads::SetProcessorAffinityOfCurrentThread(0); ->
>> pthread_setaffinity_np(...)
>>
>> // Create Threads
>> for (int i = 0; i < 100; ++i) pthread_create(...)
>>
>> viewer.run()
>> pthread_join(...);
>> }
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
More information about the osg-users
mailing list