thread()

Structure

Description

Runs a function on a new detached thread. Accepts a lambda or std::function.

Syntax

void thread(std::function<void()> fn)

Parameters

NameTypeDescription
fnstd::functionthe function to run on a separate thread

Returns

void

Related

Under the Hood

From Processing.h:

inline void thread(std::function<void()> fn) { std::thread(fn).detach(); } inline void thread(std::function<void()> fn) { std::thread(fn).detach(); void thread(std::function<void()> fn) { std::thread(fn).detach(); } void thread(std::function<void()> fn) { std::thread(fn).detach();

Under the Hood

From Processing.cpp:

std::thread([img, path]{ // Resolve search paths same as loadImage // Check PROCESSING_SKETCH_PATH env var set by IDE std::string _sketchDir; if (const char* _sp = std::getenv("PROCESSING_SKETCH_PATH")) _sketchDir = std::string(_sp) + "/"; // Also get the directory of the running executable std::string _exeDir; { char _buf[4096] = {}; #ifdef _WIN32 GetModuleFileNameA(nullptr, _buf, sizeof(_buf)); std::string _ep(_buf); size_t _sl = _ep.find_last_of("\\\\"); #else ssize_t _len = readlink("/proc/self/exe", _buf, sizeof(_buf)-1); if (_len > 0) _buf[_len] = 0; std::string _ep(_buf); size_t _sl = _ep.find_last_of("/"); #endif if (_sl != std::string::npos) _exeDir = _ep.substr(0, _sl+1); } std::vector<std::string> tries = { path, "data/" + path, "files/" + path, }; std::string found; for (auto& t : tries) { FILE* f = fopen(t.c_str(), "rb"); if (f) { fclose(f); found = t; break; } } if (found.empty()) { // Not found: mark as failed (width=0) img->width = 0; img->height = 0; std::cerr << "requestImage: file not found: " << path << "\n"; return; } #ifdef PROCESSING_HAS_STB_IMAGE int w=0, h=0, ch=0; unsigned char* data = stbi_load(found.c_str(), &w, &h, &ch, 4); if (!data || w<=0 || h<=0) { img->width = 0; img->height = 0; std::cerr << "requestImage: failed to decode: " << path << "\n"; return; } img->pixels.resize((size_t)w * h); for (int i = 0; i < w*h; i++) { unsigned char r=data[i*4+0], g=data[i*4+1], b=data[i*4+2], a=data[i*4+3]; img->pixels[i] = ((unsigned int)a<<24)|((unsigned int)r<<16)| ((unsigned int)g<<8)|(unsigned int)b; } stbi_image_free(data); img->dirty = true; // Write width/height last so the sketch sees a consistent state img->height = h; img->width = w; // width != -1 signals "done" #else img->width = 0; img->height = 0; std::cerr << "requestImage: rebuild with -DPROCESSING_HAS_STB_IMAGE: " << path << "\n"; #endif }).detach();