std::thread
Use case
Parallel analysis of binary segments.
Explanation
std::thread runs a function in a new thread. Pass the function and its arguments to the constructor. Call join() to wait for completion. Use std::mutex with std::lock_guard to prevent mixed output.
Code
#include <iostream>
#include <mutex>
#include <thread>
std::mutex printMutex;
void analyze(const char *segment) {
std::lock_guard<std::mutex> lock(printMutex);
std::cout << "Thread " << std::this_thread::get_id() << " analyzing "
<< segment << "\n";
}
int main() {
std::thread t1(analyze, "__TEXT");
std::thread t2(analyze, "__DATA");
t1.join();
t2.join();
std::cout << "Done.\n";
return 0;
}
Output
$ ./src/c++11/build/std-thread
Thread 0x16f573000 analyzing __TEXT
Thread 0x16f5ff000 analyzing __DATA
Done.