Previously I’ve done a bit of work on developing two functions that allow me to run simple tasks in parallel; (i.e. parallelising a for loop) - as a solution for no longer having openMP support on a mac with Xcode 6/Yosemite. The solution I stumbled upon after much fiddling appears to work well for basic instances and is explained here:
I’ve recently returned to the app to try applying the functions to run a task on members of a vector array. This causes problems - I get an EX_BAD_ACCESS error for each thread as it gets to the point of operating the function on a particular vector member.
Can anyone help me out with this or explain why this is happening? A zip of the code in question is attached here: concurrency.zip (71.7 KB)
in my experience things like vector are very not thread safe, emphasis on the word very. like even whispering the name of a vector in another thread leading to a crash. Maybe you need some sort of mutex to block on access. ofScopedLock() is helpful for writing this kind of code quickly.
Thanks for the response. I’ve never used ofScopedLock() or really got far into threading at all. Can you give me any pointers as to how I might apply it in this situation?
I’m not Zach, but I’ll do my best.
I suggest reading this: http://pocoproject.org/slides/130-Threads.pdf to understand the basics of multi threading.
The POCO libraries are embedded inside OF so you can use them directly (that’s what I usually do).
I haven’t checked the code, but I’m willing to bet that ofScopedLock wraps around Poco::Mutex:ScopedLock. This is a nice mechanism that makes sure that other threads won’t interrupt the work and will only be allowed to continue after the critical section (where the scoped lock was defined) is done. For this to make sense, you need to know what a mutex is. Start by reading the link.
thanks for this + the link is useful. I’ve dabbled with threads before but it all ended up a bit like a kitten with a ball of string. That’s why I’m trying to use Apple’s Grand Central Dispatch in the app mentioned.
Turns out the issue was with how I was how I was passing to the function that I wrote - solved by igiso’s suggestion here Passing function as parameter