Your application hangs because the threads are sleeping on their semaphores. You should first make sure the "flag_stop" will really stop them without making one of the threads sleep on its semaphore because of an empty/full buffer - if the "flag_stop" is set when the thread is already sleeping, it might never be woken up.
The bottom line is, you have to go through your synchronisation mechanism again and rethink/correct it.
For example this could work:
void MainWindowImpl::Stop_Thread()
{
flag_stop = true;
producer->release();
consumer->release();
producer->wait();
consumer->wait();
}
void Producer::run()
{
i=0;
while(!flag_stop)
{
if(flag_read)
{
freeBytes.acquire();
if(flag_stop) return;
int n = read_str.size();
buffer[i % 8096] = (*((read_str.toUtf8().constData())+(n-1)));
i++;
usedBytes.release();
flag_read = false;
}
}
flag_stop = true;
}
void Consumer::run()
{
j=0;
while(!flag_stop)
{
usedBytes.acquire();
if(flag_stop) return;
write_str += buffer[j % 8096];
j++;
freeBytes.release();
emit disp(); // disp a slot of mainwindow.cpp
}
flag_stop = true;
}
void MainWindowImpl::Stop_Thread()
{
flag_stop = true;
producer->release();
consumer->release();
producer->wait();
consumer->wait();
}
void Producer::run()
{
i=0;
while(!flag_stop)
{
if(flag_read)
{
freeBytes.acquire();
if(flag_stop) return;
int n = read_str.size();
buffer[i % 8096] = (*((read_str.toUtf8().constData())+(n-1)));
i++;
usedBytes.release();
flag_read = false;
}
}
flag_stop = true;
}
void Consumer::run()
{
j=0;
while(!flag_stop)
{
usedBytes.acquire();
if(flag_stop) return;
write_str += buffer[j % 8096];
j++;
freeBytes.release();
emit disp(); // disp a slot of mainwindow.cpp
}
flag_stop = true;
}
To copy to clipboard, switch view to plain text mode
As for the other problem - make sure you call Stop_Thread() before closing the application.
Bookmarks