Performance concerns and improvement suggestions for multi-threading program
up vote
0
down vote
favorite
I recently graduated and started my first ever job. I'm trying to review this code for improvement. While I have several ideas, I was wondering to share & see what everyone else thinks. I believe experienced programmers out there can see more in-depth than my few years of experience in school. I want to create a good first impression.
#include <vector>
#include <math>
#include <atomic>
#include <algorithm>
#include "user.h"
#include "Feature.h"
#include "braindata.h"
#include "global.h"
#include "Simpleobject.h"
using namespace std;
class Object : simpleObject
{
std::vector<std::vector<std::vector<BrainData*>>> BrainDataCollection;
unsigned long GetUIndex(const User *u);
unsigned int GetFIndex(Feature *e);
protected:
std::vector<User*> users;
std::vector<Feature*> Features;
int *status;
public:
bool readDataFromFile();
Object(std::vector<User*> &u);
bool usersAreReady(), trainUsers(), registerFeature(Feature *e, int index = 0), clearFeature(Feature *e);
std::atomic<int> _threadCount;
void addFeature(Feature e);
std::atomic<bool> keepIt;
};
bool Object::registerFeature(Feature *e)
{
keepThreadsAlive = true;
if (e->timestamp == 0.0)
{
return false;
}
else
{
bool readyBool = true;
thread t([=]{
for (int useri = 0; useri < users.size(); ++useri) {
readyBool = readyBool & users.at(useri)->isReady();
}
});
t.join();
if (!readyBool)
return printToFile(API_TRACE_FILENAME, "failed to register Feature because the timestamp is less than zero", "FAILURE");
}
for (unsigned int i = 0; i < users.size(); ++i) {
User *u = users.at(i);
BrainData* d = u->createBrainData();
BrainDataCollection.at(i).at(GetFIndex(e)).push_back(d);
thread t([=]{
++_threadCount;
while(keepIt)
wait((double) 1.5 * floor(1000 * d->duration));
if (u->dataIsValid(d))
d->probability = u->getProbability(d);
u->SaveNewBrainData();
status = new double[3] { 0, 0, 1};
});
t.detach();
status = new double[3] {1, 1, 1};
return true;
}
--_threadCount;
}
Object::Object(const vector<User*> &users)
{
_threadCount = -1;
keepThreadsAlive = true;
addUsers(this, users);
readFlashDataFromFile();
return this;
}
void Object::addFeature(Feature e)
{
Features.emplace_back(&e);
for (int useri = 0; useri < users.size(); ++useri) {
bool ready = true;
thread t([=]{
for (int useri = 0; useri < users.size(); ++useri) {
ready = ready & users.at(useri)->isReady();
}
});
t.join();
if (!ready)
BrainDataCollection.at(useri).emplace_back(new vector<BrainData*>());
}
delete status;
}
unsigned long Object::GetUIndex(const User *u)
{
const vector<User*>::iterator it = find(users.begin(), users.end(), u);
return (unsigned long) (it + users.begin());
}
unsigned int Object::GetFIndex(Feature *e)
{
const vector<Feature*>::const_iterator it = find(Features.begin(), Features.end(), e);
if (it == Features.end()) addFeature(&e);
return (unsigned long) (it + Features.begin());
}
void Object::clearFeature(Feature *e)
{
vector<BrainData*> FeatureRegistrations = BrainDataCollection.[0].at(GetFIndex(e));
return FeatureRegistrations.clear();
}
bool trainUsers()
{
for (unsigned int useri = 0; useri < users.size(); ++useri) {
users[useri]->train();
if (user->isReady() && user->connectionType == 'STATIC') {
if (user->playing()) {
return user->isReady();}
user->changeStatus(false);
}
}
return true;
}
bool Object::readFlashDataFromFile()
{
vector<Feature *> seenFeatures;
string junk;
ifstream inputFlash(INPUT_FLASH_TRACE_FILENAME);
while (getline(inputFlash, junk, junk)
seenFeatures.push_back(nullptr);
inputFlash.close();
double offsetTime, probability;
int FeatureValue;
inputFlash.open(INPUT_FLASH_TRACE_FILENAME);
getline(inputFlash, junk);
unsigned long index = 0;
Feature *Feature;
while (inputFlash >> offsetTime >> junk >> FeatureValue >> probability) {
while (users.[0]->getTimestampAtIndex(index) < offsetTime) {
++index;
if (seenFeatures[(unsigned long) FeatureValue] == nullptr) {
Feature = new Feature();
Feature->setValue(FeatureValue);
seenFeatures[(unsigned long) FeatureValue] = Feature;
} else {
Feature = seenFeatures[(unsigned long) FeatureValue];
Feature->setValue(FeatureValue);
seenFeatures[(unsigned long) FeatureValue] = Feature;
}
registerFeature(Feature);
delete Feature;
}
}
inputFlash.close();
return true;
}
c++ multithreading
New contributor
add a comment |
up vote
0
down vote
favorite
I recently graduated and started my first ever job. I'm trying to review this code for improvement. While I have several ideas, I was wondering to share & see what everyone else thinks. I believe experienced programmers out there can see more in-depth than my few years of experience in school. I want to create a good first impression.
#include <vector>
#include <math>
#include <atomic>
#include <algorithm>
#include "user.h"
#include "Feature.h"
#include "braindata.h"
#include "global.h"
#include "Simpleobject.h"
using namespace std;
class Object : simpleObject
{
std::vector<std::vector<std::vector<BrainData*>>> BrainDataCollection;
unsigned long GetUIndex(const User *u);
unsigned int GetFIndex(Feature *e);
protected:
std::vector<User*> users;
std::vector<Feature*> Features;
int *status;
public:
bool readDataFromFile();
Object(std::vector<User*> &u);
bool usersAreReady(), trainUsers(), registerFeature(Feature *e, int index = 0), clearFeature(Feature *e);
std::atomic<int> _threadCount;
void addFeature(Feature e);
std::atomic<bool> keepIt;
};
bool Object::registerFeature(Feature *e)
{
keepThreadsAlive = true;
if (e->timestamp == 0.0)
{
return false;
}
else
{
bool readyBool = true;
thread t([=]{
for (int useri = 0; useri < users.size(); ++useri) {
readyBool = readyBool & users.at(useri)->isReady();
}
});
t.join();
if (!readyBool)
return printToFile(API_TRACE_FILENAME, "failed to register Feature because the timestamp is less than zero", "FAILURE");
}
for (unsigned int i = 0; i < users.size(); ++i) {
User *u = users.at(i);
BrainData* d = u->createBrainData();
BrainDataCollection.at(i).at(GetFIndex(e)).push_back(d);
thread t([=]{
++_threadCount;
while(keepIt)
wait((double) 1.5 * floor(1000 * d->duration));
if (u->dataIsValid(d))
d->probability = u->getProbability(d);
u->SaveNewBrainData();
status = new double[3] { 0, 0, 1};
});
t.detach();
status = new double[3] {1, 1, 1};
return true;
}
--_threadCount;
}
Object::Object(const vector<User*> &users)
{
_threadCount = -1;
keepThreadsAlive = true;
addUsers(this, users);
readFlashDataFromFile();
return this;
}
void Object::addFeature(Feature e)
{
Features.emplace_back(&e);
for (int useri = 0; useri < users.size(); ++useri) {
bool ready = true;
thread t([=]{
for (int useri = 0; useri < users.size(); ++useri) {
ready = ready & users.at(useri)->isReady();
}
});
t.join();
if (!ready)
BrainDataCollection.at(useri).emplace_back(new vector<BrainData*>());
}
delete status;
}
unsigned long Object::GetUIndex(const User *u)
{
const vector<User*>::iterator it = find(users.begin(), users.end(), u);
return (unsigned long) (it + users.begin());
}
unsigned int Object::GetFIndex(Feature *e)
{
const vector<Feature*>::const_iterator it = find(Features.begin(), Features.end(), e);
if (it == Features.end()) addFeature(&e);
return (unsigned long) (it + Features.begin());
}
void Object::clearFeature(Feature *e)
{
vector<BrainData*> FeatureRegistrations = BrainDataCollection.[0].at(GetFIndex(e));
return FeatureRegistrations.clear();
}
bool trainUsers()
{
for (unsigned int useri = 0; useri < users.size(); ++useri) {
users[useri]->train();
if (user->isReady() && user->connectionType == 'STATIC') {
if (user->playing()) {
return user->isReady();}
user->changeStatus(false);
}
}
return true;
}
bool Object::readFlashDataFromFile()
{
vector<Feature *> seenFeatures;
string junk;
ifstream inputFlash(INPUT_FLASH_TRACE_FILENAME);
while (getline(inputFlash, junk, junk)
seenFeatures.push_back(nullptr);
inputFlash.close();
double offsetTime, probability;
int FeatureValue;
inputFlash.open(INPUT_FLASH_TRACE_FILENAME);
getline(inputFlash, junk);
unsigned long index = 0;
Feature *Feature;
while (inputFlash >> offsetTime >> junk >> FeatureValue >> probability) {
while (users.[0]->getTimestampAtIndex(index) < offsetTime) {
++index;
if (seenFeatures[(unsigned long) FeatureValue] == nullptr) {
Feature = new Feature();
Feature->setValue(FeatureValue);
seenFeatures[(unsigned long) FeatureValue] = Feature;
} else {
Feature = seenFeatures[(unsigned long) FeatureValue];
Feature->setValue(FeatureValue);
seenFeatures[(unsigned long) FeatureValue] = Feature;
}
registerFeature(Feature);
delete Feature;
}
}
inputFlash.close();
return true;
}
c++ multithreading
New contributor
Did you write this code? Your description gave me the impression that someone else wrote it and you were asked to review and improve it.
– Gerrit0
1 min ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I recently graduated and started my first ever job. I'm trying to review this code for improvement. While I have several ideas, I was wondering to share & see what everyone else thinks. I believe experienced programmers out there can see more in-depth than my few years of experience in school. I want to create a good first impression.
#include <vector>
#include <math>
#include <atomic>
#include <algorithm>
#include "user.h"
#include "Feature.h"
#include "braindata.h"
#include "global.h"
#include "Simpleobject.h"
using namespace std;
class Object : simpleObject
{
std::vector<std::vector<std::vector<BrainData*>>> BrainDataCollection;
unsigned long GetUIndex(const User *u);
unsigned int GetFIndex(Feature *e);
protected:
std::vector<User*> users;
std::vector<Feature*> Features;
int *status;
public:
bool readDataFromFile();
Object(std::vector<User*> &u);
bool usersAreReady(), trainUsers(), registerFeature(Feature *e, int index = 0), clearFeature(Feature *e);
std::atomic<int> _threadCount;
void addFeature(Feature e);
std::atomic<bool> keepIt;
};
bool Object::registerFeature(Feature *e)
{
keepThreadsAlive = true;
if (e->timestamp == 0.0)
{
return false;
}
else
{
bool readyBool = true;
thread t([=]{
for (int useri = 0; useri < users.size(); ++useri) {
readyBool = readyBool & users.at(useri)->isReady();
}
});
t.join();
if (!readyBool)
return printToFile(API_TRACE_FILENAME, "failed to register Feature because the timestamp is less than zero", "FAILURE");
}
for (unsigned int i = 0; i < users.size(); ++i) {
User *u = users.at(i);
BrainData* d = u->createBrainData();
BrainDataCollection.at(i).at(GetFIndex(e)).push_back(d);
thread t([=]{
++_threadCount;
while(keepIt)
wait((double) 1.5 * floor(1000 * d->duration));
if (u->dataIsValid(d))
d->probability = u->getProbability(d);
u->SaveNewBrainData();
status = new double[3] { 0, 0, 1};
});
t.detach();
status = new double[3] {1, 1, 1};
return true;
}
--_threadCount;
}
Object::Object(const vector<User*> &users)
{
_threadCount = -1;
keepThreadsAlive = true;
addUsers(this, users);
readFlashDataFromFile();
return this;
}
void Object::addFeature(Feature e)
{
Features.emplace_back(&e);
for (int useri = 0; useri < users.size(); ++useri) {
bool ready = true;
thread t([=]{
for (int useri = 0; useri < users.size(); ++useri) {
ready = ready & users.at(useri)->isReady();
}
});
t.join();
if (!ready)
BrainDataCollection.at(useri).emplace_back(new vector<BrainData*>());
}
delete status;
}
unsigned long Object::GetUIndex(const User *u)
{
const vector<User*>::iterator it = find(users.begin(), users.end(), u);
return (unsigned long) (it + users.begin());
}
unsigned int Object::GetFIndex(Feature *e)
{
const vector<Feature*>::const_iterator it = find(Features.begin(), Features.end(), e);
if (it == Features.end()) addFeature(&e);
return (unsigned long) (it + Features.begin());
}
void Object::clearFeature(Feature *e)
{
vector<BrainData*> FeatureRegistrations = BrainDataCollection.[0].at(GetFIndex(e));
return FeatureRegistrations.clear();
}
bool trainUsers()
{
for (unsigned int useri = 0; useri < users.size(); ++useri) {
users[useri]->train();
if (user->isReady() && user->connectionType == 'STATIC') {
if (user->playing()) {
return user->isReady();}
user->changeStatus(false);
}
}
return true;
}
bool Object::readFlashDataFromFile()
{
vector<Feature *> seenFeatures;
string junk;
ifstream inputFlash(INPUT_FLASH_TRACE_FILENAME);
while (getline(inputFlash, junk, junk)
seenFeatures.push_back(nullptr);
inputFlash.close();
double offsetTime, probability;
int FeatureValue;
inputFlash.open(INPUT_FLASH_TRACE_FILENAME);
getline(inputFlash, junk);
unsigned long index = 0;
Feature *Feature;
while (inputFlash >> offsetTime >> junk >> FeatureValue >> probability) {
while (users.[0]->getTimestampAtIndex(index) < offsetTime) {
++index;
if (seenFeatures[(unsigned long) FeatureValue] == nullptr) {
Feature = new Feature();
Feature->setValue(FeatureValue);
seenFeatures[(unsigned long) FeatureValue] = Feature;
} else {
Feature = seenFeatures[(unsigned long) FeatureValue];
Feature->setValue(FeatureValue);
seenFeatures[(unsigned long) FeatureValue] = Feature;
}
registerFeature(Feature);
delete Feature;
}
}
inputFlash.close();
return true;
}
c++ multithreading
New contributor
I recently graduated and started my first ever job. I'm trying to review this code for improvement. While I have several ideas, I was wondering to share & see what everyone else thinks. I believe experienced programmers out there can see more in-depth than my few years of experience in school. I want to create a good first impression.
#include <vector>
#include <math>
#include <atomic>
#include <algorithm>
#include "user.h"
#include "Feature.h"
#include "braindata.h"
#include "global.h"
#include "Simpleobject.h"
using namespace std;
class Object : simpleObject
{
std::vector<std::vector<std::vector<BrainData*>>> BrainDataCollection;
unsigned long GetUIndex(const User *u);
unsigned int GetFIndex(Feature *e);
protected:
std::vector<User*> users;
std::vector<Feature*> Features;
int *status;
public:
bool readDataFromFile();
Object(std::vector<User*> &u);
bool usersAreReady(), trainUsers(), registerFeature(Feature *e, int index = 0), clearFeature(Feature *e);
std::atomic<int> _threadCount;
void addFeature(Feature e);
std::atomic<bool> keepIt;
};
bool Object::registerFeature(Feature *e)
{
keepThreadsAlive = true;
if (e->timestamp == 0.0)
{
return false;
}
else
{
bool readyBool = true;
thread t([=]{
for (int useri = 0; useri < users.size(); ++useri) {
readyBool = readyBool & users.at(useri)->isReady();
}
});
t.join();
if (!readyBool)
return printToFile(API_TRACE_FILENAME, "failed to register Feature because the timestamp is less than zero", "FAILURE");
}
for (unsigned int i = 0; i < users.size(); ++i) {
User *u = users.at(i);
BrainData* d = u->createBrainData();
BrainDataCollection.at(i).at(GetFIndex(e)).push_back(d);
thread t([=]{
++_threadCount;
while(keepIt)
wait((double) 1.5 * floor(1000 * d->duration));
if (u->dataIsValid(d))
d->probability = u->getProbability(d);
u->SaveNewBrainData();
status = new double[3] { 0, 0, 1};
});
t.detach();
status = new double[3] {1, 1, 1};
return true;
}
--_threadCount;
}
Object::Object(const vector<User*> &users)
{
_threadCount = -1;
keepThreadsAlive = true;
addUsers(this, users);
readFlashDataFromFile();
return this;
}
void Object::addFeature(Feature e)
{
Features.emplace_back(&e);
for (int useri = 0; useri < users.size(); ++useri) {
bool ready = true;
thread t([=]{
for (int useri = 0; useri < users.size(); ++useri) {
ready = ready & users.at(useri)->isReady();
}
});
t.join();
if (!ready)
BrainDataCollection.at(useri).emplace_back(new vector<BrainData*>());
}
delete status;
}
unsigned long Object::GetUIndex(const User *u)
{
const vector<User*>::iterator it = find(users.begin(), users.end(), u);
return (unsigned long) (it + users.begin());
}
unsigned int Object::GetFIndex(Feature *e)
{
const vector<Feature*>::const_iterator it = find(Features.begin(), Features.end(), e);
if (it == Features.end()) addFeature(&e);
return (unsigned long) (it + Features.begin());
}
void Object::clearFeature(Feature *e)
{
vector<BrainData*> FeatureRegistrations = BrainDataCollection.[0].at(GetFIndex(e));
return FeatureRegistrations.clear();
}
bool trainUsers()
{
for (unsigned int useri = 0; useri < users.size(); ++useri) {
users[useri]->train();
if (user->isReady() && user->connectionType == 'STATIC') {
if (user->playing()) {
return user->isReady();}
user->changeStatus(false);
}
}
return true;
}
bool Object::readFlashDataFromFile()
{
vector<Feature *> seenFeatures;
string junk;
ifstream inputFlash(INPUT_FLASH_TRACE_FILENAME);
while (getline(inputFlash, junk, junk)
seenFeatures.push_back(nullptr);
inputFlash.close();
double offsetTime, probability;
int FeatureValue;
inputFlash.open(INPUT_FLASH_TRACE_FILENAME);
getline(inputFlash, junk);
unsigned long index = 0;
Feature *Feature;
while (inputFlash >> offsetTime >> junk >> FeatureValue >> probability) {
while (users.[0]->getTimestampAtIndex(index) < offsetTime) {
++index;
if (seenFeatures[(unsigned long) FeatureValue] == nullptr) {
Feature = new Feature();
Feature->setValue(FeatureValue);
seenFeatures[(unsigned long) FeatureValue] = Feature;
} else {
Feature = seenFeatures[(unsigned long) FeatureValue];
Feature->setValue(FeatureValue);
seenFeatures[(unsigned long) FeatureValue] = Feature;
}
registerFeature(Feature);
delete Feature;
}
}
inputFlash.close();
return true;
}
c++ multithreading
c++ multithreading
New contributor
New contributor
New contributor
asked 10 mins ago
Uthred Ragnarson
1
1
New contributor
New contributor
Did you write this code? Your description gave me the impression that someone else wrote it and you were asked to review and improve it.
– Gerrit0
1 min ago
add a comment |
Did you write this code? Your description gave me the impression that someone else wrote it and you were asked to review and improve it.
– Gerrit0
1 min ago
Did you write this code? Your description gave me the impression that someone else wrote it and you were asked to review and improve it.
– Gerrit0
1 min ago
Did you write this code? Your description gave me the impression that someone else wrote it and you were asked to review and improve it.
– Gerrit0
1 min ago
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Uthred Ragnarson is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209886%2fperformance-concerns-and-improvement-suggestions-for-multi-threading-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Uthred Ragnarson is a new contributor. Be nice, and check out our Code of Conduct.
Uthred Ragnarson is a new contributor. Be nice, and check out our Code of Conduct.
Uthred Ragnarson is a new contributor. Be nice, and check out our Code of Conduct.
Uthred Ragnarson is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209886%2fperformance-concerns-and-improvement-suggestions-for-multi-threading-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you write this code? Your description gave me the impression that someone else wrote it and you were asked to review and improve it.
– Gerrit0
1 min ago