Skip to content

Commit 37c2a68

Browse files
author
Daniel Kroening
committed
manual: link lock-example1.c
1 parent 2e9b181 commit 37c2a68

File tree

2 files changed

+63
-14
lines changed

2 files changed

+63
-14
lines changed

doc/cprover-manual/cbmc-tutorial.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,40 +209,48 @@ obtained with the option `--property`.
209209

210210
### Unbounded Loops
211211

212-
CBMC can also be used for programs with unbounded loops. In this case,
213-
CBMC is used for bug hunting only; CBMC does not attempt to find all
214-
bugs. The following program (lock-example.c) is an example of a program
215-
with a user-specified property:
212+
CBMC can also be used for programs with unbounded loops. In this case, CBMC
213+
is used for bug hunting only; CBMC does not attempt to find all bugs. The
214+
following program
215+
([lock-example.c](https://raw.githubusercontent.com/diffblue/cbmc/develop/doc/cprover-manual/lock-example.c))
216+
is an example of a program with a user-specified property:
216217

217218
```C
218219
_Bool nondet_bool();
219220
_Bool LOCK = 0;
220221

221-
_Bool lock() {
222-
if(nondet_bool()) {
222+
_Bool lock()
223+
{
224+
if(nondet_bool())
225+
{
223226
assert(!LOCK);
224-
LOCK=1;
225-
return 1; }
227+
LOCK = 1;
228+
return 1;
229+
}
226230

227231
return 0;
228232
}
229233

230-
void unlock() {
234+
void unlock()
235+
{
231236
assert(LOCK);
232-
LOCK=0;
237+
LOCK = 0;
233238
}
234239

235-
int main() {
240+
int main()
241+
{
236242
unsigned got_lock = 0;
237243
int times;
238244

239-
while(times > 0) {
240-
if(lock()) {
245+
while(times > 0)
246+
{
247+
if(lock())
248+
{
241249
got_lock++;
242250
/* critical section */
243251
}
244252

245-
if(got_lock!=0)
253+
if(got_lock != 0)
246254
unlock();
247255

248256
got_lock--;

doc/cprover-manual/lock-example.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
_Bool nondet_bool();
2+
_Bool LOCK = 0;
3+
4+
_Bool lock()
5+
{
6+
if(nondet_bool())
7+
{
8+
assert(!LOCK);
9+
LOCK = 1;
10+
return 1;
11+
}
12+
13+
return 0;
14+
}
15+
16+
void unlock()
17+
{
18+
assert(LOCK);
19+
LOCK = 0;
20+
}
21+
22+
int main()
23+
{
24+
unsigned got_lock = 0;
25+
int times;
26+
27+
while(times > 0)
28+
{
29+
if(lock())
30+
{
31+
got_lock++;
32+
/* critical section */
33+
}
34+
35+
if(got_lock != 0)
36+
unlock();
37+
38+
got_lock--;
39+
times--;
40+
}
41+
}

0 commit comments

Comments
 (0)