简体   繁体   English

Win32 CRITICAL_SECTION for C ++的轻量级,跨平台替代品?

[英]Lightweight, Cross-Platform Alternatives to Win32 CRITICAL_SECTION for C++?

Are there lightweight, cross-platform alternatives to Win32 CRITICAL_SECTION for C++? 是否有Win32 CRITICAL_SECTION for C ++的轻量级,跨平台替代品? I am trying to make my Windows application platform agnostic, but std::recursive_mutex is way slower than CRITICAL_SECTION. 我试图使我的Windows应用程序平台不可知,但std :: recursive_mutex比CRITICAL_SECTION慢。 I am currently using Visual Studio 2013 Community. 我当前正在使用Visual Studio 2013社区。

You should have a look at the Boost.Thread library and boost::recursive_mutex in particular. 您应该看看Boost.Thread库,尤其是boost :: recursive_mutex。

(also see How do I make a critical section with Boost? ) (另请参阅如何使用Boost做一个关键部分?

http://en.cppreference.com/w/cpp/atomic/atomic_flag "A spinlock mutex can be implemented in userspace using an atomic_flag" http://en.cppreference.com/w/cpp/atomic/atomic_flag “可以在用户空间中使用atomic_flag实现自旋锁互斥体”

I adapted their userspace spinlock mutex to allow recursive locks. 我调整了他们的用户空间自旋锁互斥锁以允许递归锁。

Warning: speedcoded synchronization logic should be assumed faulty until tested in a thousand battles, and also carefully coded synchronization logic 警告:在经过一千场战斗测试之前,应假定速度编码的同步逻辑有故障,并且还应仔细编码同步逻辑

#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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与 win32 CRITICAL_SECTION 相比的 std::mutex 性能 - std::mutex performance compared to win32 CRITICAL_SECTION stdin / stdout / stderr上的跨平台(linux / Win32)非阻塞C ++ IO - Cross-platform (linux/Win32) nonblocking C++ IO on stdin/stdout/stderr 是否有win32 CopyFile的跨平台版本? - Is there a cross-platform version of win32 CopyFile? 有没有C ++轻量级跨平台文件系统库? - Is there any C++ lightweight cross-platform file system library? 在Win32中是否有一个C ++交叉平台“命名事件,如”CreateEvent()“? - Is there a C++ cross platform “named event like the ”CreateEvent()" in Win32? 是否有跨平台持续集成的工具(c ++ Win32和linux) - Is there a tool for cross platform continuous integration (c++ Win32 and linux) 是否可以在Win32和MacOSX系统上使用跨平台的unicode字符串类? - is there a cross-platform unicode string class that I can use both on Win32 and MacOSX systems? 将 C++ 代码从 Win32 平台更改为 WinCE - change C++ code from Win32 platform to WinCE Win32关键部分与互斥性能 - Win32 Critical Section vs Mutex Performance 哪个跨平台预处理器定义? (__ WIN32__或__WIN32或WIN32)? - Which Cross Platform Preprocessor Defines? (__WIN32__ or __WIN32 or WIN32 )?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM