Home > Software engineering >  How to implement multithreaded QT console mode?
How to implement multithreaded QT console mode?

Time:11-12

Recently in a simulated memory allocator function, anyone want to according to the process of creating a group on the execution time to delete the execution of the process on time, but because the mian function inside me is in accordance with the while loop and cin to create the process, if can't achieve in the process of waiting for cin time calculation, feeling can only use multithreading to achieve, a thread to calculate time, wait a thread to execute cin, but when I was in the realization of simple version, window mode seems to be no way to simultaneously,

This is my definition Mythread, _______________________________________

# # ifndef MYTHREAD_H
# define MYTHREAD_H

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

The class MyThread: public QThread
{
Q_OBJECT;

Public:
MyThread (vector & Vec_1, time_t & amp; Start, time_t & amp; Mid) {
Refer_vec=& amp; Vec_1;
Refer_start=& amp; The start;
Refer_mid=& amp; Mid;
Cout<" Difference me the size "& lt; }


Void the run () {
While (is_runnable) {

* refer_mid=time (nullptr);
If ((* refer_mid) & gt; (* refer_start) + 3) {
(* refer_vec). Erase (refer_vec (*). The begin ());
* refer_start=time (nullptr);
}

If ((* refer_vec). The size ()==0) {
Is_runnable=false;
}
}

If (is_runnable==false) {
return;
}

}

Void stop_sub () {
Is_runnable=false;
}

Private:
Bool is_runnable=true;
Vector * refer_vec;
Time_t * refer_start;
Time_t * refer_mid;

};

# endif//MYTHREAD_H



The following is the main function: _____________________________________

#include
#include
#include
#include
#include

# include "mythread. H"
using namespace std;


Int main () {
Time_t start=time (nullptr);
Time_t end;

Vector Vec_1;
Vec_1. Push_back (4);
Vec_1. Push_back (5);
Vec_1. Push_back (6);
Vec_1. Push_back (9);
Vec_1. Push_back (8);
Vec_1. Push_back (10);
Vec_1. Push_back (19);



String yes_no;
String cre_free;
The string size.

While (true) {
Time_t mid=time (nullptr);


MyThread abcd (vec_1, start, mid);
The abcd. Start ();
The abcd. The run ();

Getline (cin, yes_no);
{if (yes_no=="yes")
The abcd. Stop_sub ();
The abcd. Quit ();
The abcd. Wait ();
break;
} else {
The abcd. Stop_sub ();
The abcd. Quit ();
The abcd. Wait ();
}


}
}
My idea is the while loop every time open a new thread, in the thread according to the time to delete the elements in the vec_1, at the same time, every time a while loop using getline (cin, yes_no) generated by the thread to the end of time, in the next cycle of generating new thread to handle the delete operation, but the window in the background is the first time every time the while loop vec_1 to delete the elements in the light to cin. Is my writing has a problem or window mode cannot realize multithreading?
  • Related