skip to main content

Procedure Division Statements : SYNCHRONIZED

SYNCHRONIZED
General Format
  [ ON Sync-Var ]
  [END-SYNCHRONIZED]
Syntax Rules
1.
2.
General rules
1.
2.
 
Examples
The following code may set the item varx to a unexpected value because it’s not safe to access the same data items from different threads:
 working-storage section.
 77 t1     handle of thread.
 77 t2     handle of thread.
 77 t3     handle of thread.
 77 varx   pic XX.
 procedure division.
    move "AA" to varx
    perform thread p1 handle t1.
    perform thread p2 handle t2.
    perform thread p3 handle t3.
    ACCEPT OMITTED
    STOP RUN
     perform UNTIL 1 = 2
       MOVE "AA" TO varx
     end-perform.   
     perform UNTIL 1 = 2
       MOVE "BB" TO varx
     end-perform.   
     perform UNTIL 1 = 2
       IF varx = "AA" or "BB"
          display "Unexpected condition! varx=" varx
     end-perform
Synchronizing the access to varx as follows, resolves the issue:
 working-storage section.
 77 t1     handle of thread.
 77 t2     handle of thread.
 77 t3     handle of thread.
 77 varx   pic XX.
 procedure division.
    move "AA" to varx
    perform thread p1 handle t1.
    perform thread p2 handle t2.
    perform thread p3 handle t3.
    ACCEPT OMITTED
    STOP RUN
     perform UNTIL 1 = 2
       SYNCHRONIZED
       MOVE "AA" TO varx
       END-SYNCHRONIZED
     end-perform.   
     perform UNTIL 1 = 2
       SYNCHRONIZED
       MOVE "BB" TO varx
       END-SYNCHRONIZED
     end-perform.   
     perform UNTIL 1 = 2
       SYNCHRONIZED
       IF varx = "AA" or "BB"
          display "Unexpected condition! varx=" varx
       END-SYNCHRONIZED
     end-perform
The following is a similar case that reproduces a NullPointerException instead of setting items to a undefined value:
 working-storage section.
 77 t1     handle of thread.
 77 t2     handle of thread.
 77 t3     handle of thread.
 01 vars.
    03 var1   pic 9(3).
    03 var2   pic 9(5).
 procedure division.
    move 1 to var1
    move 2 to var2
    perform thread P1 handle t1.
    perform thread P2 handle t2.
    perform thread P3 handle t3.
    ACCEPT OMITTED
    STOP RUN
     perform UNTIL 1 = 2
       MOVE 1 TO var1
     end-perform.   
     perform UNTIL 1 = 2
       MOVE 2 TO var2
     end-perform.   
     perform UNTIL 1 = 2
       add 1 to var1
       add 1 to var2
     end-perform.
Also in this case, the SYNCHRONIZED statement allows to fix the issue:
 working-storage section.
 77 t1     handle of thread.
 77 t2     handle of thread.
 77 t3     handle of thread.
 01 vars.
    03 var1   pic 9(3).
    03 var2   pic 9(5).
 procedure division.
    move 1 to var1
    move 2 to var2
    perform thread P1 handle t1.
    perform thread P2 handle t2.
    perform thread P3 handle t3.
    ACCEPT OMITTED
    STOP RUN
     perform UNTIL 1 = 2
       SYNCHRONIZED
       MOVE 1 TO var1
       END-SYNCHRONIZED
     end-perform.   
     perform UNTIL 1 = 2
       SYNCHRONIZED
       MOVE 2 TO var2
       END-SYNCHRONIZED
     end-perform.   
     perform UNTIL 1 = 2
       SYNCHRONIZED
       add 1 to var1
       add 1 to var2
       END-SYNCHRONIZED
     end-perform.

Copyright (c) 2017 Veryant
Contact us
Please share your comments on this manual or on any
Veryant product documentation with the email button at the top left