Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to simulate exceptions in C with goto?
    primarykey
    data
    text
    <p>I'm writing a concurrent transaction library in C and found the following problem. Let's consider a sample transaction member pseudo-code, where the "transaction" represents a communication channel with the transaction master:</p> <pre><code>transaction = trans_join(); do_some_ops(); /* receive the data from the master */ trans_rcv(transaction, data); do_some_ops(); trans_send(transaction, answer); /* wait for the possibility for voting */ trans_ready(transaction); /* vote for commiting and wait for the voting results. */ if(trans_commit(answer)) { printf("Final commiting the changes.\n"); } else { printf("Rolling back the changes.\n"); } </code></pre> <p>In concurrent transactions, we can vote only if we are asked for the vote by the master. However, the master can call <code>trans_abort(member)</code> at any time, forcing the specified member to cancel the transaction. The ABORT message can be received by the member at any stage of execution and in this case unsually it should not wait till the execution reaches <code>trans_ready()</code> call. For example, if there is a <code>trans_rcv()</code> call in the later code, the process would hang on waiting for the data from the master which will be never sent.</p> <p>Now, the point. I have already the code to register the aborting function that rolls back the changes, but I would also like to have an extra mechanism that would allow to skip the rest of the remaining operations and jump to the voting code immediately. I have an idea to use goto here to simulate exceptions:</p> <pre><code>if(!trans_rcv()) /* fail, we received the abort message */ { goto abort_code; } ... abort_code: trans_ready(transaction); /* etc. */ </code></pre> <p>However, writing <strong>ifs</strong> for every call of <code>trans_rcv</code> or <code>trans_send</code> is not very comfortable, especially if the transaction code is complex. Have you got any idea for a better solution or is this the only way? It does not have to use <code>goto</code>, by the way :).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload