import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=272;bottom=203;topmost=1)
winform.add(
button={cls="button";text="无锁演示";left=27;top=132;right=120;bottom=184;z=2};
button2={cls="button";text="有锁演示";left=139;top=132;right=232;bottom=184;z=3};
edit={cls="edit";text="0";left=54;top=26;right=206;bottom=109;edge=1;font=LOGFONT(h=-16);multiline=1;z=1}
)
/*}}*/
import godking.lock;
import raw.struct;
var lock = godking.lock();
var data = ..raw.struct({int v});
winform.button.oncommand = function(id,event){
winform.button.disabled = true;
winform.button2.disabled = true;
for(i=1;5;1){
thread.invoke(
function(data){
while(1){
import console;
console.dump("正在写=====");
thread.delay(1000);
data.v++;
console.dump("已写完");
}
},data
)
}
thread.invoke(
function(data,winform){
while(1){
thread.delay(1000);
winform.edit.text = data.v;
}
},data,winform
)
}
winform.button2.oncommand = function(id,event){
winform.button.disabled = true;
winform.button2.disabled = true;
// 创建 5 个线程,使用【写锁】,进行数据写入。
for(i=1;5;1){
thread.invoke(
function(data,lock){
import godking.lock;
import console;
lock = godking.lock(lock);
while(1){
lock.write();
console.dump("--------------------进入【写锁】,数值 +1 √√√√√√√√√√√√√√√√√√√√√√");
thread.delay(1000);
data.v++;
console.dump("即将退出【写锁】");
lock.endWrite();
}
},data,lock
)
}
// 创建 1 个线程,使用【读锁】,进行数据读取。
thread.invoke(
function(data,winform,lock){
import godking.lock;
import console;
lock = godking.lock(lock);
while(1){
lock.read();
console.dump("--------------------进入【读锁】,此时不在【写锁】内,数值是不会 +1 的。-------------");
thread.delay(1000);
winform.edit.text = data.v;
console.dump("即将退出【读锁】");
lock.endRead();
}
},data,winform,lock);
// 创建 1 个线程,不加锁,进行数据读取。
thread.invoke(
function(data,winform){
import console;
while(1){
console.dump("无锁读,不受锁影响");
thread.delay(200);
winform.edit.text = data.v;
}
},data,winform);
}
winform.show();
win.loopMessage();