Mutex experiments
This commit is contained in:
16
experiments/can_send.cpp
Normal file
16
experiments/can_send.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "can_send.h"
|
||||
#include <future>
|
||||
#include <picout.h>
|
||||
|
||||
int main() {
|
||||
auto time1 = std::async(std::launch::deferred, [] { return test_send(PCAN_USBBUS1); });
|
||||
auto time2 = std::async(std::launch::deferred, [] { return test_send(PCAN_USBBUS2); });
|
||||
|
||||
time1.wait();
|
||||
piCout << "measurements for PCAN_USBBUS1:" << time1.get() / 1000.f << "ms";
|
||||
|
||||
time2.wait();
|
||||
piCout << "measurements for PCAN_USBBUS2:" << time2.get() / 1000.f << "ms";
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
experiments/can_send.h
Normal file
23
experiments/can_send.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef MULTITHREAD_EXPERIMENTS_CAN_SEND_H
|
||||
#define MULTITHREAD_EXPERIMENTS_CAN_SEND_H
|
||||
|
||||
#include "pcan/peakcandevice.h"
|
||||
#include <chrono>
|
||||
|
||||
float test_send(int device_id) {
|
||||
PeakCANDevice canDevice(PeakCANDevice::CAN_SPEED_500K, device_id);
|
||||
if (!canDevice.open()) {
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
CAN_Raw msg = { .Id = 0x72, .Size = 8, .Data = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 } };
|
||||
for (int i = 0; i < 30 * 1000; ++i) {
|
||||
canDevice.send(msg);
|
||||
}
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.f;
|
||||
}
|
||||
|
||||
#endif //MULTITHREAD_EXPERIMENTS_CAN_SEND_H
|
||||
13
experiments/can_send_multithread.cpp
Normal file
13
experiments/can_send_multithread.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "can_send.h"
|
||||
#include <future>
|
||||
#include <picout.h>
|
||||
|
||||
int main() {
|
||||
auto time1 = std::async(std::launch::async, [] { return test_send(PCAN_USBBUS1); });
|
||||
auto time2 = std::async(std::launch::async, [] { return test_send(PCAN_USBBUS2); });
|
||||
|
||||
piCout << "measurements for PCAN_USBBUS1:" << time1.get() / 1000.f << "ms";
|
||||
piCout << "measurements for PCAN_USBBUS2:" << time2.get() / 1000.f << "ms";
|
||||
|
||||
return 0;
|
||||
}
|
||||
58
experiments/mutex.cpp
Normal file
58
experiments/mutex.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <pimutex.h>
|
||||
#include <piconditionlock.h>
|
||||
#include <atomic>
|
||||
#include <future>
|
||||
#include <picout.h>
|
||||
|
||||
std::future<float> check_performance(std::function<void()> test_function) {
|
||||
return std::async(std::launch::deferred, [=](){
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
for (int i = 0; i < 1000 * 1000; ++i) {
|
||||
test_function();
|
||||
}
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.f;
|
||||
});
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto withoutSyncPerformance = check_performance([](){
|
||||
int i = 0; while (i < 1000) i++;
|
||||
});
|
||||
piCout << "without synchronization:" << withoutSyncPerformance.get() << "ms";
|
||||
|
||||
|
||||
PIMutex piMutex;
|
||||
auto piMutexPerformance = check_performance([&piMutex](){
|
||||
piMutex.lock();
|
||||
int i = 0; while (i < 1000) i++;
|
||||
piMutex.unlock();
|
||||
});
|
||||
piCout << "piMutex:" << piMutexPerformance.get() << "ms";
|
||||
|
||||
PIConditionLock piConditionLock;
|
||||
auto piConditionLockPerformance = check_performance([&piConditionLock](){
|
||||
piConditionLock.lock();
|
||||
int i = 0; while (i < 1000) i++;
|
||||
piConditionLock.unlock();
|
||||
});
|
||||
piCout << "piConditionLock:" << piConditionLockPerformance.get() << "ms";
|
||||
|
||||
std::mutex stdMutex;
|
||||
auto stdMutexPerformance = check_performance([&stdMutex](){
|
||||
stdMutex.lock();
|
||||
int i = 0; while (i < 1000) i++;
|
||||
stdMutex.unlock();
|
||||
});
|
||||
piCout << "stdMutex:" << stdMutexPerformance.get() << "ms";
|
||||
|
||||
std::atomic_flag stdAtomic;
|
||||
auto stdAtomicPerformance = check_performance([&stdAtomic](){
|
||||
while(stdAtomic.test_and_set(std::memory_order_acquire)) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
int i = 0; while (i < 1000) i++;
|
||||
stdAtomic.clear(std::memory_order_release);
|
||||
});
|
||||
piCout << "stdAtomic:" << stdAtomicPerformance.get() << "ms";
|
||||
}
|
||||
71
experiments/mutex_multithread.cpp
Normal file
71
experiments/mutex_multithread.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <pimutex.h>
|
||||
#include <piconditionlock.h>
|
||||
#include <atomic>
|
||||
#include <future>
|
||||
#include <picout.h>
|
||||
#include <vector>
|
||||
|
||||
float check_performance(std::function<long(long&)> test_function) {
|
||||
long k = 0;
|
||||
|
||||
std::vector<std::future<float>> perThreadPerformance;
|
||||
for (int j = 0; j < 6; ++j) {
|
||||
auto future = std::async(std::launch::async, [=, &k](){
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
while (test_function(k) < 1000 * 1000) { }
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.f;
|
||||
});
|
||||
perThreadPerformance.push_back(std::move(future));
|
||||
}
|
||||
|
||||
float res = 0;
|
||||
for (int j = 0; j < perThreadPerformance.size(); ++j) {
|
||||
res += perThreadPerformance[j].get();
|
||||
}
|
||||
return res / perThreadPerformance.size();
|
||||
}
|
||||
|
||||
int main() {
|
||||
PIMutex piMutex;
|
||||
auto piMutexPerformance = check_performance([&piMutex](long& k){
|
||||
piMutex.lock();
|
||||
int i = 0; while (i < 1000) { i++; }
|
||||
long res = ++k;
|
||||
piMutex.unlock();
|
||||
return res;
|
||||
});
|
||||
piCout << "piMutex:" << piMutexPerformance << "ms";
|
||||
|
||||
PIConditionLock piConditionLock;
|
||||
auto piConditionLockPerformance = check_performance([&piConditionLock](long& k){
|
||||
piConditionLock.lock();
|
||||
int i = 0; while (i < 1000) { i++; }
|
||||
long res = ++k;
|
||||
piConditionLock.unlock();
|
||||
return res;
|
||||
});
|
||||
piCout << "piConditionLock:" << piConditionLockPerformance << "ms";
|
||||
|
||||
std::mutex stdMutex;
|
||||
auto stdMutexPerformance = check_performance([&stdMutex](long& k){
|
||||
stdMutex.lock();
|
||||
int i = 0; while (i < 1000) { i++; }
|
||||
long res = ++k;
|
||||
stdMutex.unlock();
|
||||
return res;
|
||||
});
|
||||
piCout << "stdMutex:" << stdMutexPerformance << "ms";
|
||||
|
||||
std::atomic_flag stdAtomic;
|
||||
auto stdAtomicPerformance = check_performance([&stdAtomic](long& k){
|
||||
while(stdAtomic.test_and_set(std::memory_order_acquire)) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
int i = 0; while (i < 1000) { i++; }
|
||||
long res = ++k;
|
||||
stdAtomic.clear(std::memory_order_release);
|
||||
return res;
|
||||
});
|
||||
piCout << "stdAtomic:" << stdAtomicPerformance << "ms";
|
||||
}
|
||||
Reference in New Issue
Block a user