I have two qthreads the first one is producer which writes in the buffer and the second one is consumer which reads from the buffer. I have used these commands to activate the threads so that they work simultaneously, but the threads do not start working. If I only activate the producer then the producer works. Can anyone help? these are my codes.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui/QWidget>
#include "ui_mainwindow.h"
#include "search.h"
#include <QString>
#include <QStringList>
#include <QThread>
#include "search2.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
private slots:
void receive_from_object(QStringList);
void receive_from_object1(QStringList);
void receive_from_object2(QStringList);
private:
Ui::MainWindow *ui;
QThread* thread1 ;
QThread* thread2 ;
Search* mySearch;
Search2* mySearch2;
};
#endif // MAINWINDOW_H
mainwndow.cpp
#include "mainwindow.h"
#include "search.h"
#include "search2.h"
#include <QString>
#include <QStringList>
#include <QDebug>
#include<qtimer.h>
#include <QtConcurrentRun>
#include <QMessageBox>
#include <QThread>
#include<QMessageBox>
#include<iostream>
#include "ui_mainwindow.h"
QCoreApplication app(...);
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
thread1 = new QThread();
thread2 = new QThread();
mySearch = new Search() ;
mySearch2 = new Search2() ;
mySearch->connect(mySearch,SIGNAL(transmit_to_gui(QStringList)),this,SLOT(receive_from_object(QStringList)));
mySearch2->connect(mySearch2,SIGNAL(transmit_to_gui1(QStringList)),this,SLOT(receive_from_object1(QStringList)));
mySearch2->connect(mySearch2,SIGNAL(transmit_to_gui2(QStringList)),this,SLOT(receive_from_object2(QStringList))); //ui->textEdit_2->append(freq);
mySearch->connect(mySearch2, SIGNAL(workRequested2()), SLOT(producer1()));
mySearch2->connect(mySearch, SIGNAL(workRequested()), SLOT(consumer1()));
mySearch->moveToThread(thread1);
mySearch2->moveToThread(thread2);
mySearch->connect(thread1, SIGNAL(started()), SLOT(producer1()));
//mySearch2->connect(thread2, SIGNAL(started()),mySearch2, SLOT(consumer1()));
thread1->connect(mySearch, SIGNAL(finishedscan()), SLOT(quit()));
thread2->connect(mySearch2, SIGNAL(finishedscan2()), SLOT(quit()));
thread1->start();
thread2->start();
}
MainWindow::~MainWindow()
{
}
void MainWindow::receive_from_object(QStringList MIB_List )
{
}
void MainWindow::receive_from_object1(QStringList MIB_List )
{
}
void MainWindow::receive_from_object2(QStringList Mode_List )
{
}
search.cpp
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <QDebug>
#include "global1.h"
#include "search.h"
#include <QTimer>
#include <QEventLoop>
#include <QThread>
#include <QString>
#include <QStringList>
#include <QWaitCondition>
#include <math.h>
#include <sys/time.h>
#include <assert.h>
#include <signal.h>
#define ENABLE_AGC_DEFAULT
#include <pthread.h>
enum receiver_state { one, two, three } state;
#define BUF_SIZE 1000
#define BUF_SIZE2 1000
uint8_t temp1[4]={0x7a, 0x1b, 0x6a, 0x40};
uint8_t temp2[22]={0x60, 0x50, 0xc8, 0x25, 0x98, 0x5b, 0x55, 0x73, 0x30, 0x18, 0x17, 0x54, 0xc4, 0x40, 0x82, 0x02, 0x11, 0x54, 0x00, 0x00, 0x00, 0x00};
uint8_t temp3[7]={0x40, 0x00, 0x8f, 0x0b, 0x87, 0x05, 0x60};
Search::Search(QWidget *parent) :
QWidget(parent)
{
_working = false;
_abort = false;
}
Search::~Search()
{
delete ui;
}
void Search::requestWork(){
global:: mutex.lock();
_working = true ;
_abort = false;
global::mutex.unlock();
emit workRequested();
}
void Search::abort(){
global:: mutex.lock();
if(_working){
_abort = true ;
}
global::mutex.unlock();
}
void Search :: producer1(){
int i=0;
int n=0;
state=one;
for (int j=0; j<50; j++){
/* Insert into buffer */
global:: mutex.lock();
if(BUF_SIZE-global::cnt>=100){
switch (state) {
case one:
n=4;
while((global::cnt+n)>=global::cnt2 && global::round1>global::round2)
global::c_prod.wait(&global::mutex);
global::Buffer_n[i]=n;
memcpy(&global::Buffer[global::cnt], temp1, n*sizeof(uint8_t));
global::Buffer_state[i]=one;
i++;
emit workRequested();
printf("one written in Buffer \n") ;
global::cnt=global::cnt+n*sizeof(uint8_t);
state=two;
break;
case two:
n=22;
while((global::cnt+n)>=global::cnt2 && global::round1>global::round2)
global::c_prod.wait(&global::mutex);
global::Buffer_n[i]=n;
memcpy(&global::Buffer[global::cnt], temp2, n*sizeof(uint8_t));
global::Buffer_state[i]=two;
i++;
emit workRequested();
printf("two written in Buffer \n") ;
global::cnt=global::cnt+n*sizeof(uint8_t);
state=three;
break;
case three:
n=7;
while((global::cnt+n)>=global::cnt2 && global::round1>global::round2)
global::c_prod.wait (&global::mutex);
global::Buffer_n[i]=n;
memcpy(&global::Buffer[global::cnt], temp3, n*sizeof(uint8_t));
global::Buffer_state[i]=three;
i++;
emit workRequested();
printf("three written in Buffer \n") ;
global::cnt=global::cnt+n*sizeof(uint8_t);
state=one;
break;
}
}
else{
printf ("main reset\n");
global::cnt=0;
i=0;
global::round1++;
}
global::mutex.unlock();
global::c_cons.wakeAll();
}
printf ("Parent quiting\n");
}
search2.cpp
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <QDebug>
#include "search2.h"
#include "global1.h"
#include<QTimer>
#include<QEventLoop>
#include<QThread>
#include <QString>
#include <QStringList>
#include <QWaitCondition>
#include <math.h>
#include <sys/time.h>
#include <assert.h>
#include <signal.h>
#define ENABLE_AGC_DEFAULT
#include <pthread.h>
enum receiver_state2 { one, two, three } state2;
#define BUF_SIZE 1000
#define BUF_SIZE2 1000
uint8_t Buffer2[100];
Search2::Search2(QWidget *parent) :
QWidget(parent)
{
_working = false;
_abort = false;
}
Search2::~Search2()
{
delete ui;
}
void Search2::requestWork2(){
global::mutex.lock();
_working = true ;
_abort = false;
global:: mutex.unlock();
emit workRequested2();
}
void Search2::abort2(){
global::mutex.lock();
if(_working){
_abort = true ;
}
global::mutex.unlock();
}
void Search2 :: consumer1()
{
int j=0;
uint8_t state2;
while(1){
global::mutex.lock();
if(BUF_SIZE-global::cnt2>=100){
while(global::cnt2>=global::cnt && global::round1==global::round2)
global::c_cons.wait(&global::mutex);
memcpy(Buffer2, &global::Buffer[global::cnt2], global::Buffer_n[j]*sizeof(uint8_t));
global::cnt2=global::cnt2+global::Buffer_n[j];
state2=global::Buffer_state[j];
j++;
switch (state2) {
case one:
printf("one read from buffer \n");
emit workRequested2();
break;
case two:
printf("two read from buffer \n");
break;
emit workRequested2();
case three:
printf("three read from buffer \n");
emit workRequested2();
break;
}
}else{
printf( "thread reset\n");
global::cnt2=0;
j=0;
global::round2++;
}
global::mutex.unlock();
global::c_prod.wakeAll();
}
}
Thanks in advance.
Comments
Post a Comment