userland: move more multithreading from cpp-cheat!

Convert infinite_loop.c into loop.c. Keep all examples fast by default!
This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-09-07 00:00:03 +00:00
parent 986d6cfb7b
commit e0fb39c92a
8 changed files with 165 additions and 14 deletions

View File

@@ -0,0 +1,29 @@
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-multithreading
//
// Spawn some threads and print their ID.
//
// On Ubuntu 19.04, they ar large possibly non-consecutive numbers.
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
std::mutex mutex;
void myfunc(int i) {
mutex.lock();
std::cout << i << " " << std::this_thread::get_id() << std::endl;
mutex.unlock();
}
int main() {
std::cout << "main " << std::this_thread::get_id() << std::endl;
std::vector<std::thread> threads;
for (unsigned int i = 0; i < 4; ++i) {
threads.push_back(std::thread(myfunc, i));
}
for (auto& thread : threads) {
thread.join();
}
}