0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

comelangに並列処理の銀の弾丸が入りました。

Posted at
int main(int argc,char** argv)
{
    var li = new come_mutex<list<int>*%>([1,2,3]);

    var thread2 = come {
        sleep(3);
        var it = li.lock();

        it.to_string().puts();

        li.unlock();
    }

    var thread = come {
        li.sync() {
            it.add(4);
        }
        li.sync() {
            it.add(5);
        }
    }

    come_join(thread);
    come_join(thread2);

    return 0;
}

syncは自動でlock, unlockします。ただ、メソッドブロック中はbreak, continue, returnできないため、lock, unlockを用意してます。多数の値やbreak, cotinue, returnが起こる場合, lock, unlockしてください。

この機能は正直Rustのパクリです。この機能は並列処理のエレガントな答えで、組み込みのOSでのタスク間処理でも使えると思います。要するに同一メモリに多数のスレッドやタスクからアクセスがあった場合不正なメモリアクセスが起こるので、変数単位でmutexがあれば、大丈夫ということです。

トロンの並列処理も素晴らしいですが、この言語機能はもっとすごいかもしれません。
C++やRubyやgoなどもミューテックスはありますが、変数単位でなく、自分でメモリの安全性を考えながらコードを書かないといけないため、不正なメモリアクセスが起こってしまいます。

Rustはやっぱりすごいと思います。ちょっとお袈裟ですが、これは並列処理の銀の弾丸だと思います。
comelangでも拝借させてもらいました。
まあ、他の言語でもちょっとの工夫でこのようなクラスは作れると思います。C++では簡単に書けますね。組み込みでもC++使うことが多くなってきたので、そのようなミューテックスのクラスを作ればOSのタスク間で変数を共有する場合でも競合が起きないと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?