- C/C++ code
class thread : public QThread{ void run();}thread::run(){ QFile f("test"); QTextStream textstream( &f ); textstream<<"0. Enter thread->run function!"<<'\n'; //process data1; textstream<<"1. process data1"<<'\n'; ........ //process data2; textstream<<"2. process data2"<<'\n'; ........ //process data3; textstream<<"3. process data3"<<'\n'; ........ f.close();}class a : public QWidget{ public slots: void process();}void a::process(){ QFile f("test"); QTextStream textstream( &f ); textstream<<"1. ready to start the thread!"; thread* t = new thread(); t->start(); textstream<<"2. start the thread!"; f.close();}
我在process函数中定义了一个线程变量。
第一次运行程序,在文件test中,打印了的下面的内容:
1. ready to start the thread!
2. start the thread!
0. Enter thread->run function!
1. process data1
2. process data2
3. process data3
当再次运行程序时,在文件test中,打印了下面的内容:
1. ready to start the thread!
2. start the thread!
0. Enter thread->run function!
1. process data1
0. Enter thread->run function!
1. process data1
2. process data2
3. process data3
------解决方案--------------------
线程的执行顺序是不确定的
------解决方案--------------------
是不是第二次执行的时候,test文件没有清空。还有你线程代码里的省略号是什么。