samedi 18 avril 2015

C++ multi threads

g++49 under Unix. I am not sure what i am missing here. It starts threads but does not display anything. It prints 10 default char which is dot. Any help please. //main.cpp #include "printStuff.h" #include #include #include #include #include #include #include



using namespace std;

// namespace random_delay_params
//
// values for introducing variability into the string print routine
// -. tse() - returns ticks since epoch - for seeding random delays
// -. min_to_wait, max_to_wait - bound the delay between printing a string and repeating the print (time: msec)
//
namespace random_delay_params {
unsigned long long tse(void) { return chrono::system_clock::now().time_since_epoch().count(); };
const unsigned min_to_wait = 10;
const unsigned max_to_wait = 1000;
}
template<char C>
class PrintChar : public PrintStuff {
string tempStr;
public:

PrintChar(void) : PrintStuff(delay_seed()) {

tempStr=_make_print_string();
setString(tempStr);
};
PrintChar(unsigned mx) : PrintStuff(delay_seed(), mx) {setString(tempStr); };
PrintChar(unsigned mn, unsigned mx) : PrintStuff(delay_seed(), mn, mx) {

};

~PrintChar(void) {};

private:
dre_seed_t delay_seed(void) { return static_cast<dre_seed_t>(random_delay_params::tse() + C); };
string _print_me;
string _make_print_string(void) {
string temp;
for (unsigned i = 0; i < 5; ++i) temp.push_back(C);
return temp;
}
};
void * doit( void *pPrintStuffInstance_as_void ) {
PrintStuff* pPrintStuffInstance = static_cast<PrintStuff*>(pPrintStuffInstance_as_void);
pPrintStuffInstance->go();
return NULL;
}

int main() {
using namespace random_delay_params;

PrintChar<'.'> pstuff_dot(random_delay_params::min_to_wait, random_delay_params::max_to_wait);
PrintChar<'%'> pstuff_pct(random_delay_params::min_to_wait, random_delay_params::max_to_wait);
PrintChar<'+'> pstuff_plus(random_delay_params::min_to_wait, random_delay_params::max_to_wait);
vector<PrintStuff*> pThreadCodes{ &pstuff_dot, &pstuff_plus, &pstuff_pct };
cout << "starting " << pThreadCodes.size() << " threads asynchronously" << endl;
vector<future<void>> threadFutures;
for (auto pTC : pThreadCodes) threadFutures.push_back(async([=](){pTC; }));
try {
pthread_t pdot;
pthread_t pplus;
pthread_t pper;
pthread_create(&pdot, NULL, doit, static_cast<void *>(&pstuff_dot));
pthread_create(&pplus, NULL, doit, static_cast<void *>(&pstuff_plus));
pthread_create(&pper, NULL, doit, static_cast<void *>(&pstuff_pct));
pthread_join(pdot, NULL);
pthread_join(pplus, NULL);
pthread_join(pper, NULL);
}
catch (const exception& e) {
cout << endl << "EXCEPTION: " << e.what() << endl;
}
cout << endl << "done" << endl;
}
//printStuff.cpp
#include "printStuff.h"
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include "printStuff.h"
#include <exception>
#include <string>
#include <future>
#include <algorithm>


// constructors
PrintStuff::PrintStuff(dre_seed_t seed, unsigned min_wait, unsigned max_wait) :
_dre(seed), _sleep_time(min_wait, max_wait), _printMe("."), _repCount(10) {};

// PrintStuff setters
void PrintStuff::setString(string& printMe) {_printMe = printMe;}
void PrintStuff::setRepCount(unsigned count) {_repCount = count;}
// PrintStuff::go
//
// print _printMe for _repCount number of times, pausing
// -. 40 msec between each character in each string
// -. an init-time determined number of msec between each string printing
//

void PrintStuff::go (void) {
const unsigned microSECS_per_milliSEC = 1000;
for (unsigned i = 0; i < _repCount; ++i) {
usleep(_sleep_time(_dre) * microSECS_per_milliSEC);
for (auto c : _printMe) {
usleep(40 * microSECS_per_milliSEC);
cout << c;
cout.flush();
}
}
}
//PrintStuff.h
#ifndef __printStuff_h_
#define __printStuff_h_
#include <chrono>
#include <random>
#include <string>

using namespace std;

// PrintStuff
//
// class that acts as a function object
// -. constructor - define random delay between printings of string
// -. setString() - set string to print (default: '.')
// -. setRepCount() - set repetitions to print (default: 10)
// -. go() - prints a string for a specified number of times (default: 10)
//
class PrintStuff {
public:
typedef unsigned dre_seed_t;

// constructors
//
PrintStuff(dre_seed_t = 0, unsigned min_wait = 10, unsigned max_wait = 1000);

// setters
//
void setString (string&);
void setRepCount (unsigned);

// output generation
//
void go (void);

// destructor
//
~PrintStuff() {};

private:
default_random_engine _dre;
uniform_int_distribution<int> _sleep_time;
string _printMe;
unsigned _repCount;
};

#endif

Aucun commentaire:

Enregistrer un commentaire