C++のWin32 CRITICAL_SECTIONに代わる、軽量でクロスプラットフォームの代替品はありますか?私は、私のWindowsアプリケーションプラットフォームには無関係なものにしようとしていますが、std :: recursive_mutexはCRITICAL_SECTIONよりも遅いです。私は現在、Visual Studio 2013 Communityを使用しています。C++のWin32 CRITICAL_SECTIONに対する軽量なクロスプラットフォームの代替案
答えて
具体的には、Boost.Threadライブラリとboost :: recursive_mutexを見てください。
ナットを裂くためにスレッジハンマーを使用するような気がしますが、なぜそうしないのでしょうか...わかります。 – meriken2ch
あなたの実際のニーズに応じて、より軽量の代替品ももちろんご利用いただけます。共有リソースを保護する必要がある場合は、[boost :: atomic](http://www.boost.org/doc/libs/1_59_0/doc/html/atomic/usage_examples.html)(またはstd :: atomic if C++ 11の機能を使用することもできます)もオプションになる可能性があります。 – Florian
[boost :: detail :: lightweight_mutex](http://www.boost.org/doc/libs/1_60_0/boost/detail/lightweight_mutex.hpp)は、おそらくあなたが探しているものです。ミューテックスのコンセプトの必要条件は、WindowsではCRITICAL_SECTION、POSIXシステムではpthread_mutexにマッピングされます。 – Florian
http://en.cppreference.com/w/cpp/atomic/atomic_flag 「スピンロックミューテックスはatomic_flagを使用してユーザ空間で実行することができる」
Iは、再帰的ロックを可能にするために、それらのユーザ空間スピンロックミューテックスを適合。
警告:千戦いで試験するまでspeedcoded同期ロジックが故障を想定しなければならない、とも慎重にコード化された同期ロジック
#include <thread>
#include <vector>
#include <iostream>
#include <atomic>
std::atomic_flag lock = ATOMIC_FLAG_INIT;
std::thread::id current_thread;
volatile int counter;
void lockme()
{
for(;;)
{
//protect access to current_thread and counter
while (lock.test_and_set(std::memory_order_acquire))
{}
//use current_thread and counter
//the thread with the conceptual lock is in current_thread
//if that's this thread, or no such thread, make sure this thread has the lock and increment counter
auto myid = std::this_thread::get_id();
if(current_thread == myid || current_thread == std::thread::id())
{
counter++;
current_thread = myid;
lock.clear(std::memory_order_release);
return;
}
lock.clear(std::memory_order_release);
}
}
void unlockme()
{
for(;;)
{
//protect access to current_thread and counter
while (lock.test_and_set(std::memory_order_acquire))
{}
//use current_thread and counter
//if this thread has the conceptual lock, perform the unlock
//otherwise try again
auto myid = std::this_thread::get_id();
if(current_thread == myid)
{
counter--;
if(counter==0)
current_thread = std::thread::id();
lock.clear(std::memory_order_release);
return;
}
lock.clear(std::memory_order_release);
}
}
void f(int n)
{
for (int cnt = 0; cnt < 100; ++cnt) {
for (int j = 0; j < 10; j++) lockme();
std::cout << "Output from thread " << n << '\n';
for (int j = 0; j < 10; j++) unlockme();
}
}
int main()
{
std::vector<std::thread> v;
for (int n = 0; n < 10; ++n) {
v.emplace_back(f, n);
}
for (auto& t : v) {
t.join();
}
}
- 1. Sammy.jsの軽量代替品
- 2. 軽量JMSセレクタ代替
- 3. NHibernateへの軽量の代替方法
- 4. 軽量スレッドセーフスケジューラの提案
- 5. オプションタイプの代替案C#
- 6. django-sentryの軽量代替手段は何ですか?
- 7. py2exeのクロスプラットフォームの代替
- 8. C#クロスプラットフォームRichTextBox URLまたは代替ソリューション
- 9. C++軽量コンフィグレーションライブラリ
- 10. 軽量C++スクリプティングライブラリ
- 11. 代替案
- 12. 軽量スタンドアロンC#デバッガ
- 13. Rails 3オートコンプリート - 代替案の提案?
- 14. Camel JPA @代替案
- 15. Coldfusion:CFDocument PDF代替案
- 16. GileadとRequestFactoryの代替案
- 17. AJAXコントロールツールキットの代替案ValidationCallOutExtender
- 18. jqueryの代替案msgbox
- 19. ASP.NETメンバーシップの代替案
- 20. Apache Tilesの代替案
- 21. GLUTハスケルの代替案?
- 22. ideatorrentオープンソースの代替案
- 23. コードレビューTFS 2012の代替案?
- 24. 構成ルールエンジン - 代替案?
- 25. iOSではReachabilityの代わりに軽量の代替品がありますか?
- 26. 軽量でポータブルなC++ファイバー、MITライセンス
- 27. C#の軽量エディタですか?
- 28. Apacheリソース使用量対Mongooseまたは他の軽量Webサーバ
- 29. クロスプラットフォームのインプロセスダイナミックライブラリバインディング(軽量で高性能なCOMまたはCORBA)のC++ラッパーの推奨事項
- 30. httpをサポートするC++軽量ライブラリPOST
それは再帰的ではありませんので、残念ながら、私は 'のstd :: mutex'を使用することはできません。さらに、 'std :: mutex'は' std :: recursive_mutex'と同じくらい遅いです。 – meriken2ch
あなたはstd :: atomicを調べるべきです – zeromus
'std :: atomic'を試しましたが、奇妙なBEX/BEX64例外があります。あなたが私にいくつかのコードを表示することができれば嬉しいです – meriken2ch