void ofxThreadedImage::threadedFunction(){

	#ifdef TARGET_OSX
	pthread_setname_np("ofxThreadedImage");
	#endif

	if( lock() ){

		switch (whatToDo) {
			case SAVE:
				ofSaveImage(getPixelsRef(), fileName, quality);
				break;

			case LOAD:{
				alpha = 0.0f;
				//float t1 = ofGetElapsedTimef();
				loadImageBlocking(fileName);
				//ofLog() << "time to load: " << ofGetElapsedTimef() - t1;
				}break;

			case LOAD_HTTP:
				alpha = 0;
				ofxSimpleHttp http;
				http.setTimeOut(timeOut);
				ofxSimpleHttpResponse response = http.fetchURLtoDiskBlocking(url, IMG_DOWNLOAD_FOLDER_NAME);
				if (response.ok){

					setUseTexture(false);
					bool loaded = loadImage(response.absolutePath);
					if (loaded){
						resizeIfNeeded();
						imageLoaded = true;
					}else{
						ofLog(OF_LOG_ERROR, "loadHttpImageBlocking() failed to load from disk (%d) > %s\n", response.status, url.c_str() );
					}
				}else{
					ofLog(OF_LOG_ERROR, "loadHttpImageBlocking() failed to download (%d) > %s\n", response.status, url.c_str() );
				}
				break;
		}
		unlock();

	} else {
		ofLogError("ofxThreadedImage::threadedFunction Can't %s %s, thread is already running", whatToDo == SAVE ? "Save" : "Load",  fileName.c_str() );
	}
	
	stopThread();

	#if  defined(TARGET_OSX) || defined(TARGET_LINUX) /*I'm not 100% sure of linux*/
	pthread_detach( pthread_self() ); //this is a workaround for this issue https://github.com/openframeworks/openFrameworks/issues/2506
	#endif

}
void ofxThreadedImage::threadedFunction(){

	if( lock() ){

		switch (whatToDo) {
			case SAVE:
				ofSaveImage(getPixelsRef(), fileName, quality);
				break;

			case LOAD:
				alpha = 0.0f;
				loadImageBlocking(fileName);
				break;

			case LOAD_HTTP:
				alpha = 0;
				ofxSimpleHttp http;
				http.setTimeOut(timeOut);
				ofxSimpleHttpResponse response = http.fetchURLBlocking(url);
				if (response.status == 200){

					ofDirectory dir;
					dir.open(ofToDataPath(IMG_DOWNLOAD_FOLDER_NAME, false));
					if ( !dir.exists() ){
						dir.create();
					}
					string filePath = ofToDataPath( (string)IMG_DOWNLOAD_FOLDER_NAME + "/" + response.fileName, false );
					FILE * file = fopen( filePath.c_str(), "wb");
					fwrite (response.responseBody.c_str() , 1 , response.responseBody.length() , file );
					fclose( file);
					setUseTexture(false);
					bool loaded = loadImage((string)IMG_DOWNLOAD_FOLDER_NAME + "/" + response.fileName);
					if (loaded){
						imageLoaded = true;
					}else{
						ofLog(OF_LOG_ERROR, "loadHttpImageBlocking() failed to load from disk (%d) > %s\n", response.status, url.c_str() );
					}
				}else{
					ofLog(OF_LOG_ERROR, "loadHttpImageBlocking() failed to download (%d) > %s\n", response.status, url.c_str() );
				}

				break;
		}
		unlock();

	} else {
		printf("ofxThreadedImage::threadedFunction Can't %s %s, thread is already running", whatToDo == SAVE ? "Save" : "Load",  fileName.c_str() );
	}
	stopThread();
}