Multiple threads

I understand how to make a class derived from ofThread and put my thread executions in threadFunction. Is there a way for one class to have different functions that run on different threads instead of the whole class having just one threadFunction.

I understand that poco thread also inherits from poco::runable. Do I have to rely on std::thread?

Hey @opensourceartist Lydia! I love Intel’s TBB library. tbb::parallel_for is great for crunching data in a loop. But there are lots of other threaded constructs in the library. Apress published “pro TBB” with a free ebook version. On linux, the library comes with GCC (I think), and the headers are typicall found in /usr/include/tbb/. It needs c++17 or better.

Also Apple developed GCD (Grand Central Dispatch) for macos, which is similar to TBB and maybe easier to write/read. I’m pretty sure its open source and available for linux. I’ve tried it out on a mac, but not my linux laptop.

OpenCl is also a possibility. Although I have to say that the API has quite the overhead.
its not threading the same way ofThread or any thread library is, but parallel all the same.

I am curious, are there people who use OpenCL in conjuncture with oF?

if you want to use Intel’s TBB I made an addon for it, which at the moment only has the macos compiled libs.

The only thing the addon does is that it will automatically add all the needed headers and linkage when you add it to your OF app via Project Generator. Then you use it in the regular way you use TBB. I must say that TBB is massive and you will need to read a bit in order to determine which of all its different options to use.

@hypermodernist I think that OpenCL is a bit old and not used much anymore. Not sure, just my impression.

A much easier way to have several threads running on a single class is to use C++'s std::async.
https://solarianprogrammer.com/2012/10/17/cpp-11-async-tutorial/

@TimChi @roymacdonald Thank you for pointing me to tbb. It’s quite massive but I am slowly trying to get a hang of it.

Meanwhile I’m using std::thread while learning tbb.

Is there a way to access data being continuinely written by a thread. For example, i have a detached thead running in a while true loop at intervals like this

void someThreadedFunction()
{
  ...
  while (true)
  {
    someVar = doSomeStuff;
    this_thread::sleep_until(someInverval)
  }
}

is there a way to read the new values written by the function into the someVar variable from outside the thread?

Yes, if your data are basic data types, like bool or float, you can use atomics, thus instead of declaring as

float threadVar;

you declare as

std::atomic<float> threadVar;

and then just access it from the other thread with no problem.

Otherwise you will need to use mutexes.
Read this ofBook - Threads
It has a quite thorough explanation about threads and OF.
The ofThreadedChannel class is super handy when what you need to do is to process something on another, where you need to send data from one thread to another one to be processed and not needing to worry about. There is a nice example in the exaples forlder

1 Like

Hey I was hoping that @roymacdonald would post again too, esp regarding std::thread. I’ve used ofThread, but not std::thread, so my comments may not be too helpful. The threadExample uses the std::mutex in ofThread to lock a thread so that data can be safely accessed; the std::condition_variable notifies the other threads (I think) when the lock is released. The approach used in the threadChannelExample is worth a look too, though I haven’t used it too much. But its easy to read and write and a lot of the safety happens within the class. I’ll bet that you’ve looked at both of these examples already though.

And yeah TBB is huge and it has a learning curve for sure. I have a long-term goal of learning it, and using it somewhat often so I don’t forget the nuances. I’ve used parallel_for quite a bit in oF projects, and played around a bit with the flow graphs, which I think are really interesting. With the graphs, the library will make/apply threads according to a sets of tasks, available hardware, and other scheduling parameters. Its all done pretty abstractly, so its nice for writing code that can run on different machines, and retains a task-based focus (as opposed to a thread-based focus). It has “thread-safe objects” too, like versions of containers and etc, along with some functionality from the standard library. I haven’t used these yet though.

Hi, so ofThread is a handy class what uses std::thread internally. It gives you a bunch of useful functions. Take a read at the class files, it is thoroughly commented and it is not a very large class.
Be careful with std::condition_variable as it is really tricky. The idea is that you use ofThread as the base class for another class which overrides the threadedFunction() function.