How to run a clock on its own thread - c++

I'm trying to create a step sequencer in C++ that will eventually send out MIDI data. I created it by having a clock on its own thread that calculates the amount of time since the last beat, and if it is time for the next beat, it writes a piece of data to the console.
However, I find that no matter what I set the BPM to, I get messages at a rate that is obviously too slow. I can't seem to figure out why the timing on this thread is wrong, and it doesn't help that I'm not terribly familiar with how the std::chrono library works. Thoughts?
Code below:
#include <thread>
#include <mutex>
#include <chrono>
#include <vector>
#include <iostream>
class StepSequencer {
public:
StepSequencer();
~StepSequencer();
void run();
void setBeatsPerMinute(float bpm);
void addNote(int noteValue, int beatIndex);
void playNote(int beatIndex);
protected:
int mNumberOfBeatBins;
int mSequencerPlayhead;
float mBeatsPerMinute;
float mSecondsPerBeat;
std::vector<int> mBeatBins;
std::mutex mMutex;
std::thread mSequencerThread;
bool mRunSequencerThread;
std::chrono::time_point<std::chrono::system_clock> mLastBeatTime;
std::chrono::time_point<std::chrono::system_clock> mCurrentTime;
};
#include "stdafx.h"
#include "StepSequencer.h"
StepSequencer::StepSequencer() {
mNumberOfBeatBins = 16;
for(int i = 0; i < 16; i++) {
mBeatBins.push_back(0);
}
mBeatsPerMinute = 0;
mSecondsPerBeat = 1;
mLastBeatTime = std::chrono::system_clock::now();
mCurrentTime = std::chrono::system_clock::now();
mSequencerPlayhead = 0;
mRunSequencerThread = false;
mSequencerThread = std::thread(&StepSequencer::run, this);
}
StepSequencer::~StepSequencer() {
if(mSequencerThread.joinable()) {
mSequencerThread.join();
}
}
void StepSequencer::run() {
mRunSequencerThread = true;
while(mRunSequencerThread) {
mCurrentTime = std::chrono::system_clock::now();
mMutex.lock();
if (std::chrono::duration_cast<std::chrono::seconds>(mCurrentTime - mLastBeatTime).count() > mSecondsPerBeat) {
mSequencerPlayhead++;
mSequencerPlayhead = mSequencerPlayhead % mNumberOfBeatBins;
playNote(mSequencerPlayhead);
mLastBeatTime = std::chrono::system_clock::now();
}
mMutex.unlock();
this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
void StepSequencer::setBeatsPerMinute(float bpm) {
mMutex.lock();
mBeatsPerMinute = bpm;
if(mBeatsPerMinute > 0) {
mSecondsPerBeat = 60.0 / mBeatsPerMinute;
}
else {
mSecondsPerBeat = 1;
}
mMutex.unlock();
}
void StepSequencer::addNote(int noteValue, int beatIndex) {
mBeatBins[beatIndex] = noteValue;
}
void StepSequencer::playNote(int beatIndex) {
std::cout << mBeatBins[beatIndex] << std::endl;
}

std::chrono::seconds has a representation of 'A signed integral type of at least 35 bits'. So you are going to get a value of count() which increments only once per second, giving the option of 60,30,20,15,12, etc. beats per minute.
Work in milliseconds or use a custom duration which is backed by a floating point value instead.

Related

C++ async and deferred show no difference in time compared to only async

I am creating a C++ program that uses 100 random number generators. The number generators are split into two groups: ones that create 100 numbers and ones that create 10 000 000 numbers.
I am trying to see the difference between:
Using deferred launching for the 100 numbers and async for the 10 000 000 numbers.
Using only async for both types of number generators.
There's no difference in time, so my code has something wrong with it, but so far I haven't been able to find it because I am a beginner with C++.
Below is the code. I've commented the part that uses only async.
#include <iostream>
#include <chrono>
#include <future>
#include <list>
/*
Using both deferred and async launchings: 5119 ms
Using only async launching: 5139 ms
*/
using namespace std;
class RandomNumberGenerator
{
public:
enum class task { LIGHT, HEAVY };
task taskType;
RandomNumberGenerator(): taskType(task::LIGHT)
{
int rnd = rand() % 2;
if (rnd == 0)
{
taskType = task::LIGHT;
}
else
{
taskType = task::HEAVY;
}
}
bool generateNumbers()
{
int number;
if(taskType == task::LIGHT)
{
for (int i = 0; i < 100; i++)
{
number = rand();
}
}
else
{
for (int i = 0; i < 1000000; i++)
{
number = rand();
}
}
return true;
}
};
int main()
{
cout << "Starting to generate numbers\n";
RandomNumberGenerator objects[100];
auto start = chrono::system_clock::now();
for (int i = 0; i < 100; i++)
{
objects[i].generateNumbers();
future<bool> gotNumbers;
if (objects[i].taskType == RandomNumberGenerator::task::LIGHT)
{
gotNumbers = async(launch::deferred, &RandomNumberGenerator::generateNumbers, &objects[i]);
}
else
{
gotNumbers = async(launch::async, &RandomNumberGenerator::generateNumbers, &objects[i]);
}
bool result = gotNumbers.get();
//future<bool> gotNumbers = async(launch::async, &RandomNumberGenerator::generateNumbers, &objects[i]);
//bool result = gotNumbers.get();
}
auto end = chrono::system_clock::now();
cout << "Total time = " << chrono::duration_cast<chrono::milliseconds>(end - start).count() << " seconds\n";
}
using launch::deferred or launch::async the same amount of work still needs to be done the only difference is whether it is done on another thread and the current thread blocks waiting for that thread to finish when you call gotNumbers.get() or whether the result is calculated directly in the current thread when you call gotNumbers.get(). Either way you aren't gaining any performance by using additional threads as only one thread is ever executing at a time.
If you start executing the async work before calling objects[i].generateNumbers() you might see more difference (though the overhead of std::async might still outweigh the performance increase).
#if 1
future<bool> gotNumbers;
if ( objects[ i ].taskType == RandomNumberGenerator::task::LIGHT )
{
gotNumbers = async( launch::deferred, &RandomNumberGenerator::generateNumbers, &objects[ i ] );
}
else
{
gotNumbers = async( launch::async, &RandomNumberGenerator::generateNumbers, &objects[ i ] );
}
#else
future<bool> gotNumbers = async(launch::async, &RandomNumberGenerator::generateNumbers, &objects[i]);
#endif
objects[ i ].generateNumbers();
bool result = gotNumbers.get();

Vector processing issues in multi threading

I'm implement about the data process in multi thread.
I want to process data in class DataProcess and merge the data in class DataStorage.
My problem is when the data is add to the vector sometimes occurs the exception error.
In my opinions, there have a different address class
Is it a problem to create a new data handling class and process each data?
Here is my code.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>
#include <mutex>
using namespace::std;
static std::mutex m;
class DataStorage
{
private :
std::vector<long long> vecData;
public:
DataStorage()
{
}
~DataStorage()
{
}
void SetDataVectorSize(int size)
{
vecData.clear();
vecData.resize(size);
}
void DataInsertLoop(void* Data, int start, int end)
{
m.lock();
std::vector<long long> const * _v1 = static_cast<std::vector<long long> const *>(Data);
long long num = 0;
for (int idx = start; idx < _v1->size(); ++idx)
{
vecData[idx] = _v1->at(idx);
}
m.unlock();
}
};
class DataProcess
{
private:
int m_index;
long long m_startIndex;
long long m_endIndex;
int m_coreNum;
long long num;
DataStorage* m_mainStorage;
std::vector<long long> m_vecData;
public :
DataProcess(int pindex, long long startindex, long long endindex)
: m_index(pindex), m_startIndex(startindex), m_endIndex(endindex),
m_coreNum(0),m_mainStorage(NULL), num(0)
{
m_vecData.clear();
}
~DataProcess()
{
}
void SetMainAdrr(DataStorage* const mainstorage)
{
m_mainStorage = mainstorage;
}
void SetCoreInCPU(int num)
{
m_coreNum = num;
}
void DataRun()
{
for (long long idx = m_startIndex; idx < m_endIndex; ++idx)
{
num += rand();
m_vecData.push_back(num); //<- exception error position
}
m_mainStorage->DataInsertLoop(&m_vecData, m_startIndex, m_endIndex);
}
};
int main()
{
//auto beginTime = std::chrono::high_resolution_clock::now();
clock_t beginTime, endTime;
DataStorage* main = new DataStorage();
beginTime = clock();
long long totalcount = 200000000;
long long halfdata = totalcount / 2;
std::thread t1,t2;
for (int t = 0; t < 2; ++t)
{
DataProcess* clsDP = new DataProcess(1, 0, halfdata);
clsDP->SetCoreInCPU(2);
clsDP->SetMainAdrr(main);
if (t == 0)
{
t1 = std::thread([&]() {clsDP->DataRun(); });
}
else
{
t2 = std::thread([&]() {clsDP->DataRun(); });
}
}
t1.join(); t2.join();
endTime = clock();
double resultTime = (double)(endTime - beginTime);
std::cout << "Multi Thread " << resultTime / 1000 << " sec" << std::endl;
printf("--------------------\n");
int value = getchar();
}
Interestingly, if none of your threads accesses portions of vecData accessed by another thread, DataInsertLoop::DataInsertLoop should not need to be synchonized at all. That should make processsing much faster. That is, after all bugs are fixed... This also means, you should not need a mutex at all.
There are other issues with your code... The most easily spotted is a memory leak.
In main:
DataStorage* main = new DataStorage(); // you call new, but never call delete...
// that's a memory leak. Avoid caling
// new() directly.
//
// Also: 'main' is kind of a reserved
// name, don't use it except for the
// program entry point.
// How about this, instead ?
DataStorage dataSrc; // DataSrc has a very small footprint (a few pointers).
// ...
std::thread t1,t2; // why not use an array ?
// as in:
std::vector<std::tread> thrds;
// ...
// You forgot to set the size of your data set before starting, by calling:
dataSrc.SetDataVectorSize(200000000);
for (int t = 0; t < 2; ++t)
{
// ...
// Calling new again, and not delete... Use a smart pointer type
DataProcess* clsDP = new DataProcess(1, 0, halfdata);
// Also, fix the start and en indices (NOTE: code below works for t < 2, but
// probably not for t < 3)
auto clsDP = std::make_unique<DataProcess>(t, t * halfdata, (t + 1) * halfdata);
// You need to keep a reference to these pointers
// Either by storing them in an array, or by passing them to
// the threads. As in, for example:
thrds.emplace_back([dp = std::move(clsDP)]() {clsDP->DataRun(); });
}
//...
std::for_each(thrds.begin(), thrds.end(), [](auto& t) { t.join(); });
//...
More...
You create a mutex on your very first line of executable code. That's good... somewhat...
static std::mutex m; // a one letter name is a terrible choice for a variable with
// file scope.
Apart form the name, it's not in the right scope... If you want to use a mutex to protect DataStorage::vecData, this mutex should be declared in the same scope as DataStorage::vecData.
One last thing. Have you considered using iterators (aka pointers) as arguments to DataProcess::DataProcess() ? This would simplify the code quite a bit, and it would very likely run faster.

How do I make a simple loading screen with just characters?

I'm trying to make a loading screen for a simulation I have to do, so the console isn't just blank for 10 seconds. I just want to add an asterisk to a line every 2 seconds of simulation time. Here's the code for I came up with for the loading screen.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
//initialize a random seed
srand(time(NULL));
time_t simTime=10;
time_t passedTime=0;
time_t beginTime=time(NULL);
do
{
time_t currentTime=time(NULL);
passedTime=currentTime-beginTime;
//Code for simulation
if(passedTime%2==0)
cout<<"*";
cout<<endl;
}while(passedTime<simTime);
It just prints out asterisks neverendingly for 10 seconds.
You implemented active waiting. You will need two threads: One that does the loading and one that mostly sleeps and prints the asterisc every two seconds or so. Maybe something like this:
bool loadingComplete;
void PrintLoading()
{
do
{
std::cout << '*';
std::this_thread::sleep_for(2s);
}
while(!loadingComplete);
}
void LoadStuff()
{
// Long running task
}
int main()
{
std::thread t(PrintLoading);
loadingComplete = false;
LoadStuff();
loadingComplete = true;
t.join();
}
Without actively interrupting the thread this might run 2 seconds longer than needed and I didn't have time to test, but I hope it points you in the right direction.
You don't necessarily need a separate thread if you simply want to print out the asterisks while you do your similation in your loop.
Here's an example based on your code:
#include <chrono>
#include <iostream>
using namespace std;
int main() {
float simulation_duration = 0.0;
float maximum_duration = 10.0;
auto time_since_start_or_last_asterisk = chrono::high_resolution_clock::now();
do {
//Code for simulation
auto current_time = chrono::high_resolution_clock::now();
std::chrono::duration<double> time_since_last_asterisk = current_time - time_since_start_or_last_asterisk;
if (time_since_last_asterisk.count() >= 2.0){
cout << "*";
cout.flush();
simulation_duration += time_since_last_asterisk.count();
time_since_start_or_last_asterisk = current_time;
}
} while (simulation_duration < maximum_duration);
cout << endl;
}

Processing an array of objects with multithreading - invalid use of void expression error

I need to run some number of threads to process an array of objects.
So I've written this piece of code :
unsigned int object_counter = 0;
while(object_counter != (obj_max - left))
{
thread genThread[thread_num];//create thread objects
///launch threads
int thread_index = 0;
for (; thread_index<thread_num; thread_index++)
{
genThread[thread_index] = thread(object[object_counter].gen_maps());//launch a thread
object_counter++;
if(object_counter == (obj_max - left)
{
break;
}
}
///finish threads
for (; thread_index>0; thread_index--)
{
genThread[thread_index].join();
}
}
Basically, there is an array of objects (number of objects = obj_max - left).
Each object has a function (void type function) called gen_maps() that generates a terrain.
What I want to do is running all gen_maps() functions from all objects using multithreading.
A maximum number of threads is stored in thread_num variable.
But when I'm trying to compile this code I'm getting an error:
error: invalid use of void expression
genThread[thread_index] = thread(object[object_counter].gen_maps(), thread_index);//launch a thread
^
How can I fix this issue?
A more extendable way to manage an arbitrarily large number of jobs with a smaller number of threads is to use a thread pool.
Here's a naive implementation (for better efficiency there would be 2 condition variables to manage control and state reporting) which allows the initiator to add an arbitrary number of jobs or threads and wait for all jobs to be complete.
#include <thread>
#include <condition_variable>
#include <mutex>
#include <vector>
#include <functional>
#include <deque>
#include <cassert>
#include <ciso646>
#include <iostream>
struct work_pool
{
std::mutex control_mutex;
std::condition_variable control_cv;
std::deque<std::function<void()>> jobs;
bool terminating = false;
std::size_t running = 0;
std::vector<std::thread> threads;
work_pool(std::size_t n = std::thread::hardware_concurrency())
{
add_threads(n);
}
work_pool(const work_pool&) = delete;
work_pool& operator=(const work_pool&) = delete;
~work_pool()
{
wait();
shutdown();
}
void add_threads(std::size_t n)
{
while (n--)
{
threads.emplace_back([this]{
run_jobs();
});
}
}
void run_jobs()
{
while (1)
{
auto lock = std::unique_lock(control_mutex);
control_cv.wait(lock, [this] {
return terminating or not jobs.empty();
});
if (terminating) return;
++running;
auto job = std::move(jobs.front());
jobs.pop_front();
lock.unlock();
job();
lock.lock();
--running;
lock.unlock();
control_cv.notify_one();
}
}
void shutdown()
{
auto lock = std::unique_lock(control_mutex);
terminating = true;
lock.unlock();
control_cv.notify_all();
for (auto&& t : threads) {
if (t.joinable()) {
t.join();
}
}
threads.clear();
}
void wait()
{
auto lock = std::unique_lock(control_mutex);
control_cv.wait(lock, [this] {
return jobs.empty() and not running;
});
}
template<class F>
void add_work(F&& f)
{
auto lock = std::unique_lock(control_mutex);
assert(not terminating);
jobs.emplace_back(std::forward<F>(f));
lock.unlock();
control_cv.notify_all();
}
};
// dummy function for exposition
void generate_map() {}
int main()
{
work_pool pool;
for(int i = 0 ; i < 100000 ; ++i)
pool.add_work(generate_map);
pool.wait();
// maps are now all generated
std::cout << "done" << std::endl;
}
With object[object_counter].gen_maps() you call the function gen_maps and use the returned value as the thread function. Apparently gen_maps is declared to return void which leads to the error you get.
You need to pass a pointer to the function, and then pass the object it should be called on as an argument to the thread:
thread(&SomeClass::gen_maps, object[object_counter])

Unhandled exception when using difftime()

I am creating a program that reads in data about fireworks from an XML file (such as type, colour, time in milliseconds it launches and how long it lasts for etc). This then displays the fireworks display using openGL in a loop.
My approach to this is to have a manager class, that takes in a vector of the xml read fireworks and moves them between 3 different vectors: inactiveFireworks (those that are yet to fire), activeFireworks (those that need to be animated) and depletedFireworks (those that will be pushed back to inactiveFireworks when the display is run).
To check to see if a firework needs to be fired, I need to work out the difference between the time the firework manager was called and the current time of the program and multiply that by 1000 to get milliseconds.
eg: if a firework is due to fire at 3000, it will be called 3 seconds in to the existence of the fire work manager.
Unfortunately, I get an unhandled exception error at this point and I'm not sure why...
timeDifference = difftime(time(&currentTime),initTime) * 1000;
here is the header file:
#ifndef FWORKMANAGER_H
#define FWORKMANAGER_H
#include <Time.h>
#include <vector>
#include "firework.h"
class fworkManager
{
private:
time_t initTime;
time_t currentTime;
double timeDifference;
std::vector<firework*> inactiveFireworks;
std::vector<firework*> activeFireworks;
std::vector<firework*> depletedFireworks;
public:
fworkManager(std::vector<firework*> fListIn);
void drawAllFireworks();
void evolve();
void fireInactiveFireworks();
void moveActiveFireworks();
void moveDepletedFireworks();
void reset();
};
#endif
And here is the CPP.
#include <vector>
#include "LUtil.h"
#include "fworkManager.h"
fworkManager :: fworkManager(std::vector<firework*> fListIn){
inactiveFireworks = fListIn;
time (&initTime);
}
//animates fireworks
void fworkManager::evolve(){
//check time against inactiveFireworks
fireInactiveFireworks();
moveActiveFireworks();
moveDepletedFireworks();
reset();
}
//draws fireworks as they come up
void fworkManager::drawAllFireworks()
{
std::vector<firework*>::iterator i;
for(i=activeFireworks.begin(); i != activeFireworks.end(); i ++)
{
(*i) -> draw();
}
}
//if fireworks are ready to fire, push them to active list
void fworkManager::fireInactiveFireworks()
{
timeDifference = difftime(time(&currentTime),initTime) * 1000;
std::vector<firework*>::iterator i;
for(i = inactiveFireworks.begin(); i != inactiveFireworks.end();)
{
if((*i) -> getBegin() <= timeDifference)
{
activeFireworks.push_back(*i);
(*i) -> explode();
i = inactiveFireworks.erase(i);
}else{
++i;
}
}
}
//animate each firework in the active list
void fworkManager::moveActiveFireworks()
{
std::vector<firework*>::iterator i;
for(i = activeFireworks.begin(); i != activeFireworks.end(); i++)
{
(*i) -> evolve();
}
}
//move fireworks that have met their duration requirement to the depleted list.
void fworkManager::moveDepletedFireworks()
{
std::vector<firework*>::iterator i;
for(i = activeFireworks.begin(); i != activeFireworks.end();)
{
if((*i) -> getLifeSpan() >= (*i) -> getDuration() )
{
depletedFireworks.push_back(*i);
i = activeFireworks.erase(i);
}else{
++i;
}
}
}
//repopulates the inactive firework list and resets the time difference. Allows animation to loop.
void fworkManager::reset()
{
if(inactiveFireworks.empty() && activeFireworks.empty())
{
time (&initTime);
std::vector<firework*>::iterator i;
for(i=depletedFireworks.begin(); i != depletedFireworks.end();)
{
(*i) -> reset();
inactiveFireworks.push_back(*i);
i = depletedFireworks.erase(i);
}
}
}
Many Thanks for any insight offered.