mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 10:15:57 +01:00
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:
@@ -79,7 +79,7 @@ int main(int argc, char **argv) {
|
||||
if (argc > 2) {
|
||||
niters = std::stoull(argv[2], NULL, 0);
|
||||
} else {
|
||||
niters = 10000;
|
||||
niters = 10;
|
||||
}
|
||||
std::vector<std::thread> threads(nthreads);
|
||||
for (size_t i = 0; i < nthreads; ++i)
|
||||
|
||||
29
userland/cpp/thread_get_id.cpp
Normal file
29
userland/cpp/thread_get_id.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-multithreading
|
||||
//
|
||||
// http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine
|
||||
//
|
||||
// Not affected by taskset: https://stackoverflow.com/questions/1006289/how-to-find-out-the-number-of-cpus-using-python/55423170#55423170
|
||||
|
||||
61
userland/cpp/thread_return_value.cpp
Normal file
61
userland/cpp/thread_return_value.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
// http://stackoverflow.com/questions/7686939/c-simple-return-value-from-stdthread
|
||||
// http://stackoverflow.com/questions/28950835/c-error-no-type-named-type-in-class-stdresult-ofvoid-stdunordered
|
||||
// http://stackoverflow.com/questions/21048906/stdthread-pass-by-reference-calls-copy-constructor
|
||||
// http://stackoverflow.com/questions/8299545/passing-arguments-to-thread-function
|
||||
// http://stackoverflow.com/questions/5116756/difference-between-pointer-and-reference-as-thread-parameter
|
||||
|
||||
#include <cassert>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
int myfunc(int i) {
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
void myfunc_reference(int& i) {
|
||||
i = myfunc(i);
|
||||
}
|
||||
|
||||
int main() {
|
||||
unsigned int nthreads = 4;
|
||||
std::vector<int> inputs{1, 2, 3, 4};
|
||||
std::vector<int> outputs_expect{2, 3, 4, 5};
|
||||
|
||||
// future and sync. Nirvana. When you are not fighting to death with types:
|
||||
// https://stackoverflow.com/questions/10620300/can-stdasync-be-use-with-template-functions
|
||||
{
|
||||
std::vector<std::future<int>> futures(nthreads);
|
||||
std::vector<int> outputs(nthreads);
|
||||
for (decltype(futures)::size_type i = 0; i < nthreads; ++i) {
|
||||
futures[i] = std::async(
|
||||
myfunc,
|
||||
inputs[i]
|
||||
);
|
||||
}
|
||||
for (decltype(futures)::size_type i = 0; i < nthreads; ++i) {
|
||||
outputs[i] = futures[i].get();
|
||||
}
|
||||
assert(outputs_expect == outputs);
|
||||
}
|
||||
|
||||
// Reference arguments.
|
||||
//
|
||||
// Annoying because requires:
|
||||
//
|
||||
// - wrapping the return function to accept references
|
||||
// - keeping an array of outputs
|
||||
// - std::ref
|
||||
{
|
||||
std::vector<std::thread> threads(nthreads);
|
||||
std::vector<int> inouts(inputs);
|
||||
for (decltype(threads)::size_type i = 0; i < nthreads; ++i) {
|
||||
threads[i] = std::thread(myfunc_reference, std::ref(inouts[i]));
|
||||
}
|
||||
for (auto& thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
assert(outputs_expect == inouts);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user