Click to See Complete Forum and Search --> : ZThread problem...


topdrive
June 16th, 2007, 07:08 AM
This program shutdowns when I call interrupt method. Why does this happen?
When i delete blocking operations (Mutex and sleep()) it works fine, but this should be a "demo" program on how to use interruption with blocking elements...

Please help..


#include <cstdlib>
#include <iostream>
#include <zthread/Runnable.h>
#include <zthread/Thread.h>
#include <zthread/Mutex.h>
#include <conio.h>

using namespace ZThread;

class Piszacy : public Runnable{
private:
int id;
Mutex *lock;
public:
Piszacy(int id1, Mutex* l):id(id1),lock(l) {}
void run() {
try {
while (!Thread::interrupted()) {
lock->acquire();
std::cout << id;
Thread::yield();
std::cout << std::endl;
lock->release();
Thread::sleep(300);
}
} catch(Interrupted_Exception& e) {
std::cout << "przerwanie przez wyjatek " << id << std::endl;
}
}
};

int main(int argc, char *argv[])
{
try {

Mutex lock;
Piszacy p1(1,&lock);
Piszacy p2(2,&lock);

Thread t1(&p1);
Thread t2(&p2);

getche();

t1.interrupt();
t2.interrupt();


std::cout << "zamknalem watki" << std::endl;

} catch(Synchronization_Exception& e) {
std::cout << e.what() << std::endl;
}

std::cout << "dalszy ciag programu..." << std::endl;

getche();
return EXIT_SUCCESS;
}