multiple threads and using ofImage

Hya,

**update**
Explaining a problem always helps to solve it :). Though not sure if this is correct, please tell me if not. What I’m doing now: thread B calls a function on thread A where I was loading an image. This gives me the EXC_BAD_ACCESS error. Now, I’m not loading the image anymore in the function in thread A which gets called by B, but instead I set a flag “should_update_image” in that function, then in the “update()” function in thread A I check if I should load the image again.

In pseudo code:

  
class ThreadA {  
	void update() {  
	}  
	  
	void setNewImagePath(sPath) {  
		image_path = sPath;  
		thread_b->onNewImagePath(sPath);  
	}  
	  
	string image_path  
	ThreadB thread_b;  
};  
  
class ThreadB {  
	// called from the main thread  
	void update() {   
		if(should_update) {  
			ofImage o;  
			o.loadImage(new_image_path);  
		}  
	}  
	  
	// called from thread A  
	void onNewImagePath(string sNewPath) {  
		should_update = true;  
		new_image_path = sNewPath;  
	}  
	  
	bool should_update;  
	string new_path;  
	  
};  
  

** end update**

When working with multiple threads where a thread (A) notifies another thread (B) about a new image location which should be shown/loaded in thread B, I get an EXC_BAD_ACCESS in ofTexture, where it calls glGenTextures(…) (see screenshot).

When thinking about this, I was wondering what should be the way to work with multiple threads and openGL?

Kind regards,
Roxlu

Hi Roxlu

you can’t use threaded calls to opengl, that usually results in a crash. All opengl calls must be made only on the main thread.
If you want to use ofImage on a separate thread you have to prevent it from using an opengl texture by setting

myImage.setUseTexture(false);

and then on the main thread you can create a texture and load the pixels into it if you want to draw your image.
(perhaps you could just set myImage.setUseTexture(true) on the main thread, once the image is loaded… not sure about this, would depend on how the textures are generated inside ofImage)

hope it helps

Rui