1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
Mutex mutex;
FifoMutex fifoMutex;
class MutexTest : public PACC::Threading::Thread {
int c;
float w, w2;
public:
MutexTest(int c_, float w_, float w2_) : c(c_), w(w_), w2(w2_) {}
protected:
void main() {
sleep(w);
mutex.lock();
cout << c << " got access" << endl;
sleep(w2);
mutex.unlock();
}
};
class FifoMutexTest : public PACC::Threading::Thread {
int c;
float w, w2;
public:
FifoMutexTest(int c_, float w_, float w2_) : c(c_), w(w_), w2(w2_) {}
protected:
void main() {
sleep(w);
fifoMutex.lock();
cout << c << " got access" << endl;
sleep(w2);
fifoMutex.unlock();
}
};
int main(int argc, char *argv[])
{
int nb = 10;
float sleep = 0.4;
MutexTest* mutextest[nb];
FifoMutexTest* fifotest[nb];
cout << "according to fifo rule, should be 0 -> 10" << endl;
cout << "---------- MUTEX TEST ---------" << endl;
for (int i=0; i<nb; i++) {
mutextest[i] = new MutexTest(i, i * 0.02, sleep);
mutextest[i]->run();
}
for (int i=0; i<nb; i++) {
mutextest[i]->wait();
delete mutextest[i];
}
cout << "---------- FIFO-MUTEX TEST ---------" << endl;
for (int i=0; i<nb; i++) {
fifotest[i] = new FifoMutexTest(i, i * 0.02, sleep);
fifotest[i]->run();
}
for (int i=0; i<nb; i++) {
fifotest[i]->wait();
delete fifotest[i];
}
system("pause");
} |
Partager