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

Christoph Weiss weiss at wsoptics.de
Sun Sep 25 02:44:54 PDT 2016


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(...);
>   }



More information about the osg-users mailing list