Reputation: 423
I am using QT to run a Traffic Project. In one point, i create a thread who has to set some variables, wait sometime, set the variables to another value and die; While this thread is waiting, other threads must keep running. This is the thread class
//CLASS FOR CHANGING DYNAMIC LANE
class change_lane : public QThread
{
private:
lane* dynamic_lane;
lane_marking *lm_A;
lane_marking *lm_B;
time_t change_time;
direction_way new_dir;
public:
change_lane(lane *dl, time_t ct, direction_way new_dir) :
dynamic_lane(dl), lm_A(dl->get_mark_A()), lm_B(dl->get_mark_B()), change_time(ct), new_dir(new_dir) {}
void run()
{
QMutex mux;
mux.lock();
cout << "CHANGING DYNAMIC LANE" << endl;
switch(new_dir)
{
case A:
lm_A->change_status(changing_sw);
lm_B->change_status(changing_df);
break;
case B:
lm_A->change_status(changing_df);
lm_B->change_status(changing_sw);
break;
}
sleep(change_time);
switch(new_dir)
{
case A:
lm_A->change_status(same_way);
lm_B->change_status(different_way);
break;
case B:
lm_A->change_status(different_way);
lm_B->change_status(same_way);
break;
}
dynamic_lane->setDirection(new_dir);
cout << "DONE: CHANGING DYNAMIC LANE" << endl;
mux.unlock();
}
};
This is how I call the thread;
This is inside another thread who is running in a while(true) loop;
The Thread is only called once because it sets the w->changing_lane to false;
//CHECKING IF DYNAMIC ROAD MUST CHANGE
if(w->changing_lane == true)
{
direction_way temp_dirway = w->dyn_lane->getDirection();
switch(temp_dirway)
{
case (A):
{
change_lane cl_A(w->dyn_lane,5,B);
cl_A.start();
break;
}
case (B):
{
change_lane cl_B(w->dyn_lane,5,A);
cl_B.start();
break;
}
}
w->changing_lane = false;
}
When I Press the button, I get this error:
And the program just stop responding.
In the Application Output, I get this error:
QThread: Destroyed while thread is still running QMutex: destroying locked mutex ASSERT failure in QThread::setTerminationEnabled(): "Current thread was not started with QThread.", file thread\qthread_win.cpp, line 641
Please help
Upvotes: 0
Views: 458
Reputation: 91
The parent thread controls child threads through the QThread object.Consequently the lifetime of the QThread object should be greater than the lifetime of the thread. After starting a thread you should wait for the thread to finish before deleting it. Call QThread::wait() to do this.
The mutex you've declared in the thread doesn't do anything as it is local to the thread. The point of a mutex is to use it from different threads to protect the data that is shared between them (so a mutex has to be accessible to more than one thread to be of any use).
Upvotes: 1