Hi everybody,
have some trouble with Runtime library on step run of application (compiling was successful, build too) but when I press Start have a crash with this tags:
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
terminate called after throwing an instance of ‘std::system_error’ what(): Invalid argument
QObject::killTimers: timers cannot be stopped from another thread
Use: Qt 5.3 win MinGW 4.8 ×32 but run on x64 system, maybe problem here but i’m not sure
or because I try to combine c++11 code with Qt, but not sure too, because Qt traslate all it code to C++…
cannibals.h
#ifndef CANNIBALS_H
#define CANNIBALS_H
#include <thread>
#include <mutex>
#include <time.h>
#include <windows.h>
#define M 100
#include <QObject>
{
Q_OBJECT
public:
explicit Cannibals
(QObject *parent
= 0);
void Dinner_a(int);
private:
void Cooking();
void Dinner();
std::thread Cook;
std::mutex eating;
int food=10;
signals:
void NumberChanged(int);
public slots:
};
#endif // CANNIBALS_H
#ifndef CANNIBALS_H
#define CANNIBALS_H
#include <thread>
#include <mutex>
#include <time.h>
#include <windows.h>
#define M 100
#include <QObject>
class Cannibals : public QObject
{
Q_OBJECT
public:
explicit Cannibals(QObject *parent = 0);
void Dinner_a(int);
private:
void Cooking();
void Dinner();
std::thread Cook;
std::mutex eating;
int food=10;
signals:
void NumberChanged(int);
public slots:
};
#endif // CANNIBALS_H
To copy to clipboard, switch view to plain text mode
cannibals.cpp
#include "cannibals.h"
Cannibals
::Cannibals(QObject *parent
) :{
}
void Cannibals::Cooking()
{
food=M;
}
void Cannibals::Dinner_a(int z)
{
eating.lock();
for(int i=0; i<z;i++){
Dinner();}
eating.unlock();
}
void Cannibals::Dinner()
{
if(food!=0){
emit NumberChanged(food);
--food;}
else
Cook = std::thread(&Cannibals::Cooking, this);
Cook.join();
Sleep(rand() 00);
}
#include "cannibals.h"
Cannibals::Cannibals(QObject *parent) :
QObject(parent)
{
}
void Cannibals::Cooking()
{
food=M;
}
void Cannibals::Dinner_a(int z)
{
eating.lock();
for(int i=0; i<z;i++){
Dinner();}
eating.unlock();
}
void Cannibals::Dinner()
{
if(food!=0){
emit NumberChanged(food);
--food;}
else
Cook = std::thread(&Cannibals::Cooking, this);
Cook.join();
Sleep(rand() 00);
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "cannibals.h"
#include <ui_mainwindow.h>
#include <QMainWindow>
#include <thread>
#define N 20
#define M 100
#define P (M/N)
namespace Ui {
class MainWindow;}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
Cannibals *Foo;
std::thread Cannibal[N];
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
public slots:
void OnNumberChanged (int);
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "cannibals.h"
#include <ui_mainwindow.h>
#include <QMainWindow>
#include <thread>
#define N 20
#define M 100
#define P (M/N)
namespace Ui {
class MainWindow;}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Cannibals *Foo;
std::thread Cannibal[N];
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
public slots:
void OnNumberChanged (int);
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
Foo = new Cannibals();
connect(Foo,SIGNAL(NumberChanged(int)),this,SLOT(OnNumberChanged(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
for(int i=0;i<N;i++)
{
Cannibal[i] = std::thread(&Cannibals::Dinner_a, Foo, P);
}
for(int i=0;i<N;i++)
{
Cannibal[i].join();
}
}
void MainWindow::OnNumberChanged(int Num)
{
ui->listWidget->clear();
ui->pushButton->setEnabled(false);
ui
->listWidget
->addItem
(QString(Num
) + " cannibal ate");
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Foo = new Cannibals();
connect(Foo,SIGNAL(NumberChanged(int)),this,SLOT(OnNumberChanged(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
for(int i=0;i<N;i++)
{
Cannibal[i] = std::thread(&Cannibals::Dinner_a, Foo, P);
}
for(int i=0;i<N;i++)
{
Cannibal[i].join();
}
}
void MainWindow::OnNumberChanged(int Num)
{
ui->listWidget->clear();
ui->pushButton->setEnabled(false);
ui->listWidget->addItem(QString(Num) + " cannibal ate");
}
To copy to clipboard, switch view to plain text mode
Bookmarks