new class PISemaphore
doc for PIProtectedVariable
This commit is contained in:
72
main.cpp
72
main.cpp
@@ -12,8 +12,10 @@
|
||||
#include "pitranslator_p.h"
|
||||
#include "pivaluetree_conversions.h"
|
||||
|
||||
using namespace PICoutManipulators;
|
||||
#include <windows.h>
|
||||
|
||||
using namespace PICoutManipulators;
|
||||
/*
|
||||
class MyStr: public PIString {
|
||||
public:
|
||||
MyStr(): PIString() {}
|
||||
@@ -32,16 +34,70 @@ public:
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
class RWL {
|
||||
public:
|
||||
void lockWrite() { mutex.lock(); }
|
||||
void unlockWrite() { mutex.unlock(); }
|
||||
void lockRead() {}
|
||||
void unlockRead() {}
|
||||
PIMutex mutex;
|
||||
};
|
||||
|
||||
PIMutex mutex;
|
||||
PISemaphore sem(10);
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
PIProtectedVariable<MyStr> var;
|
||||
// MyStr s("hello");
|
||||
var.set("hello");
|
||||
auto & v = var.lock();
|
||||
// v = "world";
|
||||
var.unlock();
|
||||
piCout << var.get();
|
||||
/*sem.acquire(2);
|
||||
piCout << sem.tryAcquire(2);
|
||||
piCout << sem.available();
|
||||
return 0;*/
|
||||
PIThread t_w(
|
||||
[] {
|
||||
// PIMutexLocker _ml(mutex);
|
||||
piCout << "write start ...";
|
||||
piMSleep(500);
|
||||
sem.release(11);
|
||||
piCout << "write end" << sem.available() << "\n";
|
||||
},
|
||||
true,
|
||||
1_Hz);
|
||||
|
||||
int cnt0 = 0, cnt1 = 0;
|
||||
PIThread t_r0(
|
||||
[&cnt0] {
|
||||
// PIMutexLocker _ml(mutex);
|
||||
piCout << "read0 start ...";
|
||||
piMSleep(50);
|
||||
sem.acquire(2);
|
||||
bool ok = true; // sem.tryAcquire(2, 100_ms);
|
||||
if (ok) ++cnt0;
|
||||
piCout << "read0 end" << sem.available() << ok;
|
||||
},
|
||||
true,
|
||||
10_Hz);
|
||||
PIThread t_r1(
|
||||
[&cnt1] {
|
||||
// PIMutexLocker _ml(mutex);
|
||||
piCout << "read1 start ...";
|
||||
piMSleep(50);
|
||||
sem.acquire(2);
|
||||
bool ok = true; // sem.tryAcquire(2, 100_ms);
|
||||
if (ok) ++cnt1;
|
||||
piCout << "read1 end" << sem.available() << ok;
|
||||
},
|
||||
true,
|
||||
11_Hz);
|
||||
|
||||
piSleep(5.);
|
||||
|
||||
t_r0.stopAndWait();
|
||||
t_r1.stopAndWait();
|
||||
t_w.stopAndWait();
|
||||
|
||||
piCout << cnt0 << cnt1;
|
||||
|
||||
/*PICodeParser parser;
|
||||
parser.parseFile("client_server.h");
|
||||
|
||||
Reference in New Issue
Block a user