Grand Central Dispatch

does any one know if “Grand Central Dispatch” can somehow be used through OF ?

“optimize application support for systems with multi-core processors. … It is an implementation of task parallelism based on the thread pool pattern”


from reading these, it sounds like it should be simple to to use this great way of using threads.
It seems that GCD does most of the threading work for you.

But still I am not able to get this implemented.…-0-6.ars/13…–dispatch/…-patch.html


Hello Stephan,

sorry for the late reply. What’s your problem in detail?

I only added

dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
//Just for testing, do hard work here
cout << “Test”;

to setup() in emptyExample. to check, that it runs on another thread, i added

cout << key;

to keyPressed(int key)
And it worked without problems (Key presses got recognized and print out on console, “Test” got printed after 5 seconds).
You don’t have to add anything else, it comes all for free :smiley: (Ignoring that you loose cross-platformness with this code).

Guess you have to use gcc 4.2 that it works, because of the blocks syntax.


Hey Gestalt.


It works.
But I guess it has the same problems with opengl than ofxThread has.

At least right now it gives me a bad access error.
I will keep at it.

Thanks again,

Hello Stephan,


I wouldn’t say gcd has problems with openGL, I would say openGL has problems with GCD and ofxThread as it is not thread save. So if you’re trying to do some openGL calls on another thread than main, it will NEVER work (as long as openGL is not threadsafe), reagardless what threading-technology you are using.


It should also work on Linux machines with GCD installed, however, shouldn’t it?


so GCD works fine, but I am having problems doing any kind of drawing inside it.

i know openGL draw does not work.

so i tried using ofxOpenCV
and I am getting errors like this:

error: passing ‘const ofxCvGrayscaleImage’ as ‘this’ argument of ‘virtual void ofxCvImage::allocate(int, int)’ discards qualifiers

I tried using pure openCV code and it worked but I am more comfortable using ofxOpenCv code.

Any ideas how to make this work?

 ofxCvGrayscaleImage grayImage;  
		int totalPts2;  
		CvPoint * cPts2;  
		dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);  
		dispatch_async(queue, ^{  
		grayImage.allocate(int(tailLength+mainImgWidth), tailWidth);  
		// get storage  
		 totalPts2 = nStraightVertexes; //12;  
		cPts2 = new CvPoint[nStraightVertexes+1];  
		for (int i = 0; i < nStraightVertexes; i++){  
			cPts2[i].x = (int)straightVertices[i].x;  
			cPts2[i].y = (int)straightVertices[i].y;  
		cvFillPoly(grayImage.getCvImage(), &cPts2, &totalPts2, 1,cvScalar(255,NULL,NULL,NULL) );  

this probably has something to do with the blocks stuff (the ^{ … } syntax). openCV relies on things being const, which it looks like you might have to jump through some hoops to get const stuff to work with blocks.

Hey Stephan,

I tried your code, same result, BUT:
when I declared ofxCvGrayscaleImage grayImage; in the header file it works.

Or declaring the gray image as pointer via ofxCvGrayscaleImage *grayImage, init via new
and running the functions via the -> operator also worked.

Anyone C++? My tip: It has something to do with heap and stack.