我正在用 C 编写并发事务库,发现以下问题。让我们考虑一个示例事务成员伪代码,其中“事务”表示与事务主机的通信通道:
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");
}
在并发事务中,只有当我们被主人要求投票时,我们才能投票。但是master可以随时调用trans_abort(member)
,强制指定成员取消事务。成员可以在执行的任何阶段接收到 ABORT 消息,在这种情况下,它不应该等到执行到达trans_ready()
调用。例如,如果trans_rcv()
在后面的代码中有一个调用,则该进程将一直等待来自 master 的数据,这些数据永远不会发送。
现在,重点。我已经有代码来注册回滚更改的中止函数,但我还希望有一个额外的机制,允许跳过其余的操作并立即跳转到投票代码。我有个想法在这里使用 goto 来模拟异常:
if(!trans_rcv()) /* fail, we received the abort message */
{
goto abort_code;
}
...
abort_code:
trans_ready(transaction);
/* etc. */
但是,为or的每个调用编写ifs并不是很舒服,尤其是在事务代码很复杂的情况下。您对更好的解决方案有任何想法还是这是唯一的方法?顺便说一句,它不必使用:)。trans_rcv
trans_send
goto