I need to calculate an integral using the trapezoidal rule and multithreading.
I am using a pool thread that I wrote using a java pool thread example.
#ifndef POOL_H
#define POOL_H
#include <QObject>
#include <QThread>
#include <QWaitCondition>
#include <QMutex>
#include <QQueue>
#include "poolworker.h"
#include "segment.h"
class Segment;
class PoolWorker;
class Pool: public QObject
{
Q_OBJECT
public:
explicit Pool(QObject *parent = 0);
Pool(int nThreads);
void execute(Segment *s);
static QWaitCondition con;
static QMutex poolMutex;
static QQueue<Segment*> segmentQueue;
private:
int nThreads;
QVector<PoolWorker*> workers;
};
#endif // POOL_H
#include "pool.h"
QWaitCondition Pool::con;
QQueue<Segment*> Pool::segmentQueue;
QMutex Pool::poolMutex;
Pool::Pool(QObject *parent) :
QObject(parent)
{
}
Pool::Pool(int nThreads)
{
this->nThreads = nThreads;
for (int i = 0; i < nThreads; i++)
{
workers.push_back(new PoolWorker());
workers[i]->start();
}
}
void Pool::execute(Segment *s)
{
poolMutex.lock();
segmentQueue.enqueue(s);
con.wakeOne();
poolMutex.unlock();
}
#ifndef POOLWORKER_H
#define POOLWORKER_H
#include <QThread>
#include <QMutex>
#include "segment.h"
#include "pool.h"
class PoolWorker : public QThread
{
Q_OBJECT
public:
explicit PoolWorker(QObject *parent = 0);
void run();
static QMutex mutex;
signals:
public slots:
private:
};
#endif // POOLWORKER_H
#include "poolworker.h"
QMutex PoolWorker::mutex;
PoolWorker::PoolWorker(QObject *parent) :
QThread(parent)
{
}
void PoolWorker::run()
{
Segment *temp;
forever
{
mutex.lock();
while(Pool::segmentQueue.isEmpty())
{
Pool::con.wait(&mutex);
}
temp = Pool::segmentQueue.dequeue();
mutex.unlock();
temp->doWork();
}
}
Each interval is put into a container "Segment" which also calculates the integral.
Sab = 0.5*(b-a)*(f(a)+f(b))
m = (a+b)/2.0
Sam = 0.5*(m-a)*(f(a)+f(m))
Smb = 0.5*(b-m)*(f(b)+f(m))
If the difference between Sab and Sam+Smb is lower than Eps, then I add Sab to the integral sum using Manager::addSum. If it's not lower, I do the same algorithm for am and mb. etc.
#ifndef SEGMENT_H
#define SEGMENT_H
#include <QObject>
#include <cmath>
#include "manager.h"
#include <QDebug>
class Segment : public QObject
{
Q_OBJECT
private:
double a,b,Sab,Sam,Smb,m,Eps;
double f(double x);
public:
explicit Segment(QObject *parent = 0);
Segment(double a, double b);
void doWork();
signals:
public slots:
};
#endif // SEGMENT_H
#include "segment.h"
Segment::Segment(QObject *parent) :
QObject(parent)
{
}
Segment::Segment(double a, double b)
{
this->a = a;
this->b = b;
Eps = 0.001;
}
void Segment::doWork()
{
Sab = 0.5*(b-a)*(f(a)+f(b));
m = (a+b)/2.0;
Sam = 0.5*(m-a)*(f(a)+f(m));
Smb = 0.5*(b-m)*(f(b)+f(m));
if (fabs(Sab - (Sam + Smb)) <= Eps)
{
Manager::addSum(Sab);
qDebug() << "Reached Eps on interval a= " << a << ",b = " << b
<< ", return S+= " << Sab;
Manager::inc();
}
else
{
Manager::threadPool->execute(new Segment(a,m));
Manager::threadPool->execute(new Segment(m,b));
}
}
double Segment::f(double x)
{
return pow(x,3.0) - 4.0*pow(x,2.0) + 6.0*x - 24.0;
}
The Manager class ties everything in: it creates the pool, contains the sum and starts the calculation by calling execute on pool with the first interval. It also has a counter for debugging purposes.
#ifndef MANAGER_H
#define MANAGER_H
#include <QObject>
#include <QThread>
#include <QQueue>
#include <QVector>
#include "segment.h"
#include "pool.h"
class Pool;
class Manager : public QObject
{
Q_OBJECT
private:
static double sum;
static int i;
static QMutex mutex;
public:
explicit Manager(QObject *parent = 0);
static Pool *threadPool;
static void addSum(double add);
static void inc();
double viewSum();
int viewCount();
void doSetup(QThread &thread);
signals:
public slots:
void doWork();
};
#endif // MANAGER_H
#include "manager.h"
double Manager::sum = 0;
int Manager::i = 0;
Pool* Manager::threadPool = new Pool(10);
QMutex Manager::mutex;
Manager::Manager(QObject *parent) :
QObject(parent)
{
}
void Manager::addSum(double add)
{
mutex.lock();
sum += add;
mutex.unlock();
}
void Manager::inc()
{
i++;
}
double Manager::viewSum()
{
return sum;
}
int Manager::viewCount()
{
return i;
}
void Manager::doSetup(QThread &thread)
{
connect(&thread,SIGNAL(started()),this,SLOT(doWork()));
}
void Manager::doWork()
{
threadPool->execute(new Segment(4.5,12.0));
}
In main I create the manager, a thread for the manager and display the results.
#include <QCoreApplication>
#include <QDebug>
#include <QThread>
#include <QTimer>
#include "manager.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Manager man;
QThread manThread;
man.doSetup(manThread);
man.moveToThread(&manThread);
manThread.start();
manThread.wait(2500);
qDebug() << "integrate(x^3 - 4*x^2 + 6*x - 24) from 4.5 to 12.0 = "
<< man.viewSum();
qDebug() << "i =" << man.viewCount();
manThread.quit();
QTimer::singleShot(1000, &a, SLOT(quit()));
return a.exec();
}
It calculates the integral correctly about half of the time. The other half I get a larger number than expected(which varies). When I get a larger number, I notice that some of the intervals are calculated twice. If I'm not mistaken I have made the code thread-safe so I don't understand how this happens. I'm pretty new to multithread programming so I might be doing something wrong with mutexes? Or maybe my transition from the java pool is wrong?
Another thing is in main.cpp I'm not sure how to properly display the results since I don't know when the integral is done calculating. I am using a wait(2500) function on the thread that contains the manager but it's not a really good method since the calculation time may vary on different PC's and for different functions.
Thanks in advance for any help you are able to provide.
You got your locking wrong. In the Java example you pointed to, the same lock (the queue itself) is used in enqueueing (in execute) and dequeueing (in the worker threads). That way, the queue operations are really threadsafe. In your code unfortunately, you use two different locks. Pool::poolMutex for enque (in execute) and PoolWorker::mutex for deque (in the PoolWorker thread). That way, you guard the queue only for deque between threads, but deque and enque can happen concurrently. Your Pool::poolMutex is useless because execute is being called only by one thread, so it is being locked and unlocked only by one thread. You need to use only one and the same mutex for enque and deque. Pass the Pool::poolMutex into PoolWorker via constructor and lock on it instead of PoolWorker::mutex.
So when you enque and some thread has just finished working, it will deque immediately (because queue is not empty), not waiting for your wakeOne. Then on wakeOne, you will fire off another thread. I don't have a clear explanation of how the two threads can grab the same job (and not crash), but your code will definitely work better if you use only one lock as in the Java original.
Related
I wrote a simple threadpool server with qt. When i try to connect to server on win 32/64 all works good. But when I use linux centos 7 server is not responding. I use 127.0.0.1:8080 for server address. Also server uses database mysql. When I try to connect via telnet it connects but nothing happens. I checked for open ports with netstat. Maybe I missed something because of this the server is not working?
Here is my code for server. In fact, there is also an http request handler, but it does not reach it, I tried to output a string in the constructor - it is not called.
QthreadPoolServer.cpp
#include "QThreadPoolServer.h"
#include "QSocketRunnable.h"
#include "ConfigReader.h"
#include <memory>
QThreadPoolServer::QThreadPoolServer()
{
ConfigReader reader(config_file_path);
QHostAddress server_IP(reader.getServerAddress());
int port = reader.getServerPort();
listen(QHostAddress::localhost, 8080);
std:: cout << serverError() << errorString().toStdString();
m_threadPool = std::make_shared<QThreadPool>(this);
}
void QThreadPoolServer::incomingConnection(int handle)
{
std::shared_ptr<QSocketRunnable> runnable = std::make_shared<QSocketRunnable>(handle);
runnable->setAutoDelete(false);
m_threadPool->start(runnable.get());
}
QThreadPoolServer::~QThreadPoolServer()
{
m_threadPool->~QThreadPool();
}
QThreadPoolServer.h
#ifndef QTHREADPOOLSERVER_H
#define QTHREADPOOLSERVER_H
#include <QTcpServer>
#include <QThreadPool>
#include <memory>
class QThreadPoolServer : public QTcpServer
{
public:
explicit QThreadPoolServer();
void incomingConnection(int handle);
~QThreadPoolServer();
private:
std::shared_ptr<QThreadPool> m_threadPool;
};
#endif // QTHREADPOOLSERVER_H
QSocketRunnable.cpp
#include "QSocketRunnable.h"
#include <QString>
#include <memory>
#include <iostream>
QSocketRunnable::QSocketRunnable(int handle) : m_descriptor(handle) { }
void QSocketRunnable::run()
{
QTcpSocket* socket = new QTcpSocket();
socket->setSocketDescriptor(m_descriptor);
socket->waitForReadyRead();
QString request_data = QString(socket->readAll());
HttpRequestHandler handler(request_data);
handler.makeResponse();
QString http_response_result = handler.getHttpResponse();
std::cout << http_response_result.toStdString() << "\n";
socket->write(http_response_result.toUtf8());
socket->waitForBytesWritten(90000);
socket->disconnectFromHost();
socket->close();
socket->deleteLater();
}
QSocketRunnable.h
#ifndef QSOCKETRUNNABLE_H
#define QSOCKETRUNNABLE_H
#include <QRunnable>
#include <QTcpSocket>
#include <QtDebug>
#include <QString>
//#include "IDHelper.h"
//#include "JsonFormatter.h"
//#include "HttpRequestHandler.h"
class QSocketRunnable : public QRunnable
{
public:
QSocketRunnable(int handle);
void run() override;
private:
int m_descriptor;
};
#endif // QSOCKETRUNNABLE_H
main.cpp
#include <QCoreApplication>
#include "QThreadPoolServer.h"
#include "signal.h"
#include <sstream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QThreadPoolServer server;
return a.exec();
}
Also std:: cout << serverError() << errorString().toStdString(); returns "-1" that means QAbstractSocket::UnknownSocketError -1 An unidentified error occurred.
As #chehrlic correctly noted: I had an incorrectly overloaded function, so here is the ritht version of QThreadPoolServer.h
QThreadPoolServer.h
#ifndef QTHREADPOOLSERVER_H
#define QTHREADPOOLSERVER_H
#include <QTcpServer>
#include <QThreadPool>
#include <memory>
class QThreadPoolServer : public QTcpServer
{
public:
explicit QThreadPoolServer();
protected:
void incomingConnection(qintptr handle) override;
~QThreadPoolServer();
private:
std::shared_ptr<QThreadPool> m_threadPool;
};
#endif // QTHREADPOOLSERVER_H
My my implementation did not work correctly with a smart pointer to a runnable object:
QThreadPoolServer.cpp
void QThreadPoolServer::incomingConnection(qintptr handle)
{
QSocketRunnable* runnable = new QSocketRunnable(handle)
runnable->setAutoDelete(true);
m_threadPool->start(runnable);
}
I am having two classes one main class and another server class. I am calling run method in server class as a seperate thread from main class. I need to pass values from main class to server class periodically.
I tried using std::atomic but the value in the thread is not getting changed.
Am I doing something wrong below? Any help or advice is highly appreciated. THanks.
#include <iostream>
#include "server.h"
#include "atomic"
using namespace std;
std::atomic<int> clientSignal(-1);
void foo();
int main()
{
cout << "Hello World!" << endl;
server* board;
board = new server();
thread serverThread(&server::run,*board,std::ref(clientSignal));
serverThread.join();
foo(); // the value of clientSignal is changed from within main class //based on input from sensors.
return 0;
}
void foo(){
clientSignal = 8;
}
server.h
#ifndef SERVER_H
#define SERVER_H
class server
{
public:
server();
~server();
void run(std::atomic<int> &clientSignal);
private:
std::atomic<int> *clientFlag;
};
#endif // SERVER_H
server.cpp
#include "server.h"
server::server()
{
}
server::~server()
{
}
void server::run(std::atomic<int> &clientSignal) {
clientFlag = &clientSignal;
cout << *clientFlag;
...
}
I'm newbie here, so if I have any errors just tell me.
The problem is that I have two processes and I want them to execute concurrently because they take too much time. So I thought to implement a class timer which manage its own boost::asio::io_service and create a thread for this io_service. The code is the following:
timer.hpp
#include <iostream>
#include <string>
#include <functional>
#include <thread>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
class timer
{
public:
timer(std::function<void(void)> task,
int time)
: io__(),
timer__(io__, boost::posix_time::milliseconds(time)),
repetitive_task__(task),
time_wait__(time)
{
timer__.async_wait(boost::bind(&timer::loop, this));
}
void start()
{
thread__ = std::thread([this](){
io__.run();
});
thread__.join();
}
void loop()
{
repetitive_task__();
timer__.expires_at(timer__.expires_at() + boost::posix_time::milliseconds(time_wait__));
timer__.async_wait(boost::bind(&timer::loop, this));
}
void stop()
{
timer__.cancel();
io__.stop();
}
private:
boost::asio::io_service io__;
boost::asio::deadline_timer timer__;
std::function<void(void)> repetitive_task__;
int time_wait__;
std::thread thread__;
};
For testing it, I have the simplest main I could think:
main.cpp
#include "timer.hpp"
void test1()
{
printf("action1 \n");
}
void test2()
{
printf("action 2 \n");
}
int main(int argc, char* argv[])
{
timer timer1(&test1, 100);
timer timer2(&test2, 50);
timer1.start();
timer2.start();
return 0;
}
And the result is always action1. Never action2.
I've been looking for how to implement timers properly like in this post or in this example of boost, but I still don't understand what I am doing wrong.
Thanks in advance
I have a problem in instantiating a new class with another parameter.
My problem is to add something in a QList Asteroids Object. In Qt, I have this error message:
cannot convert 'SettingsAsteroid' to 'SettingsAsteroid*' in assignment
this->settingsAsteroid = SettingsAsteroid();
Below are the relevant files of the class doing it, and I think other classes are not relevant.
Data in GameView.h :
#ifndef GAMEVIEW_H
#define GAMEVIEW_H
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QApplication>
#include <QPushButton>
#include <QList>
#include <QObject>
#include <QString>
#include "Asteroid.h"
#include "SettingsAsteroid.h"
class GameView : public QGraphicsView
{
Q_OBJECT
// Data
int nbAsteroids;
int nbAsteroidsAlive;
SettingsAsteroid* settingsAsteroid;
QList<Asteroid> asteroids;
// Menu
QPushButton *startgame;
// Scène
QGraphicsScene* grfxScene;
public:
GameView();
~GameView();
private slots:
void start();
};
#endif // GAMEVIEW_H
Source code in GameView.c :
#include "GameView.h"
#include <iostream>
GameView::GameView()
{
int nbAsteroids = 0;
int nbAsteroidsAlive = 0;
// data de jeu
this->settingsAsteroid = SettingsAsteroid();
//Scene de debut
this->grfxScene = new QGraphicsScene();
grfxScene->setSceneRect(0,0,800,600);
this->grfxScene->addPixmap(QPixmap(":/images/armageddon.jpg"));
setScene(this->grfxScene);
}
GameView::~GameView(){ }
void GameView::start()
{
this->grfxScene->clear();
int nbAsteroids = 4;
int nbAsteroidsAlive = 4;
int i;
for(i=0;i<nbAsteroids;i++) {
asteroids.append(new Asteroid(settingsAsteroid));
}
}
Constructor of Asteroid.c :
Asteroid::Asteroid(SettingsAsteroid settingsAsteroid)
Based on your error
cannot convert 'SettingsAsteroid' to 'SettingsAsteroid*' in assignment this->settingsAsteroid = SettingsAsteroid();
On the code:
this->settingsAsteroid = SettingsAsteroid();
You are attempting to convert a SettingsAsteroid into a SettingsAsteroid*, that is: a pointer to a SettingsAsteroid object.
Because GameView has a member settingsAsteroid which is a SettingsAsteroid*, you need to give it a pointer to a SettingsAsteroid, not a SettingsObject itself. You can do one of the following:
this->settingsAsteroid = new SettingsAsteroid();
Calling new will allocate memory for the required object (your SettingsAsteroid) and return a pointer to that memory, of type SettingsAsteroid*. Alternatively, if you already have some SettingsAsteroid object you could assign it instead:
SettingsAsteroid sa;
...
this->settingsAsteroid = &sa;
I'm using Gtkmm and multithreading.
I have a class "NetworkWorker" doig stuffs with the network in a secondary thread.
In this class i want to make many signals which will be handled by my class "MainWindow".
The methods which handle these signals, will edit append text in a TextView.
I have the following code:
NetworkWorker.h
#ifndef NETWORKWORKER_H_
# define NETWORKWORKER_H_
# include <sigc++/sigc++.h>
# include <glibmm/threads.h>
# include <string>
class NetworkWorker
{
public:
NetworkWorker();
~NetworkWorker();
void start();
void stop();
sigc::signal<void, std::string&>& signal_data_received();
private:
void run();
sigc::signal<void, std::string&> m_signal_data_received;
Glib::Threads::Thread* m_thread;
Glib::Threads::Mutex m_mutex;
bool m_stop;
};
#endif
NetworkWorker.c
#include <cstdlib>
#include <glibmm/timer.h>
#include <glibmm/threads.h>
#include <iostream>
#include <sigc++/sigc++.h>
#include "NetworkWorker.h"
NetworkWorker::NetworkWorker() :
m_thread(NULL), m_stop(false)
{
}
NetworkWorker::~NetworkWorker()
{
stop();
}
void NetworkWorker::start()
{
if (!m_thread)
m_thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &NetworkWorker::run));
}
void NetworkWorker::stop()
{
{
Glib::Threads::Mutex::Lock lock(m_mutex);
m_stop = true;
}
if (m_thread)
m_thread->join();
}
sigc::signal<void, std::string&>& NetworkWorker::signal_data_received()
{
return m_signal_data_received;
}
void NetworkWorker::run()
{
while (true)
{
{
Glib::Threads::Mutex::Lock lock(m_mutex);
if (m_stop)
break;
}
Glib::usleep(5000);
std::cout << "Thread" << std::endl;
std::string* str = new std::string("MyData");
m_signal_data_received.emit(*str);
}
}
MainWindow.h
#ifndef MAIN_WINDOW_H_
# define MAIN_WINDOW_H_
# include <gtkmm/textview.h>
# include <gtkmm/window.h>
# include <string>
class MainWindow : public Gtk::Window
{
public:
MainWindow();
~MainWindow();
void appendText(const std::string& str);
private:
Gtk::TextView m_text_view;
};
#endif
MainWindow.c
#include <gtkmm/notebook.h>
#include <gtkmm/widget.h>
#include <iostream>
#include <string>
#include "MainWindow.h"
MainWindow::MainWindow()
{
set_title("My App");
set_default_size(800, 600);
add(m_text_view);
}
MainWindow::~MainWindow()
{
}
void MainWindow::appendText(const std::string& str)
{
std::string final_text = str + "\n";
Glib::RefPtr<Gtk::TextBuffer> buffer = m_text_view.get_buffer();
Gtk::TextBuffer::iterator it = buffer->end();
buffer->insert(it, final_text);
Glib::RefPtr<Gtk::Adjustment> adj = m_text_view.get_vadjustment();
adj->set_value(adj->get_upper() - adj->get_page_size());
}
and my main.cpp
#include <cstdlib>
#include <gtkmm/main.h>
#include <iostream>
#include <string>
#include "MainWindow.h"
#include "NetworkWorker.h"
void recv(const std::string& str)
{
std::cout << str << std::endl;
}
int main(int argc, char **argv)
{
Gtk::Main app(Gtk::Main(argc, argv));
MainWindow main_window;
NetworkWorker network_worker;
main_window.show_all();
network_worker.signal_data_received().connect(sigc::ptr_fun(&recv));
network_worker.signal_data_received().connect(sigc::mem_fun(main_window, &MainWindow::appendText));
network_worker.start();
Gtk::Main::run(main_window);
return (EXIT_SUCCESS);
}
These snippetes have been a re-adapted for this question, so maybe some change is incoherent.
When I execute this code, I have the following output:
$> ./client
Thread
MyData
Thread
MyData
[...]
Thread
MyData
Thread
MyData
(client:5596): Gtk-CRITICAL **: gtk_text_layout_real_invalidate: assertion 'layout->wrap_loop_count == 0' failed
Thread
MyData
Thread
MyData
[...]
Thread
MyData
Thread
MyData
[1] 5596 segmentation fault (core dumped) ./client
Can some one help me to resolve this issue ? :)
The issue is you are calling non threadsafe function call (signal callbacks are not threadsafe).
So you need to use something like Glib::signal_idle().connect( sigc::mem_fun(*this, &IdleExample::on_idle) );(or whatever is equivalent to C API call g_idle_add(GCallback func)) from your thread. This function is threadsafe (at least the one from the C API).
See this tutorial for a simplified example.
Never call or signal from different threads when using UI libraries. Usually the APIs are designed to be called from a single thread. This is the single most often made mistake when using UI toolkits.