⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shared_mutex.hpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 HPP
📖 第 1 页 / 共 2 页
字号:
                        new_state.exclusive_waiting_blocked=true;                    }                    else                    {                        new_state.exclusive=true;                    }                    state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);                    if(current_state==old_state)                    {                        break;                    }                    old_state=current_state;                }                if(!old_state.shared_count && !old_state.exclusive)                {                    return true;                }                unsigned long const wait_res=detail::win32::WaitForMultipleObjects(2,semaphores,true,::boost::detail::get_milliseconds_until(wait_until));                if(wait_res==detail::win32::timeout)                {                    for(;;)                    {                        state_data new_state=old_state;                        if(new_state.shared_count || new_state.exclusive)                        {                            if(new_state.exclusive_waiting)                            {                                if(!--new_state.exclusive_waiting)                                {                                    new_state.exclusive_waiting_blocked=false;                                }                            }                        }                        else                        {                            new_state.exclusive=true;                        }                        state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);                        if(current_state==old_state)                        {                            break;                        }                        old_state=current_state;                    }                    if(!old_state.shared_count && !old_state.exclusive)                    {                        return true;                    }                    return false;                }                BOOST_ASSERT(wait_res<2);            }        }        void unlock()        {            state_data old_state=state;            for(;;)            {                state_data new_state=old_state;                new_state.exclusive=false;                if(new_state.exclusive_waiting)                {                    --new_state.exclusive_waiting;                    new_state.exclusive_waiting_blocked=false;                }                new_state.shared_waiting=0;                state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);                if(current_state==old_state)                {                    break;                }                old_state=current_state;            }            release_waiters(old_state);        }        void lock_upgrade()        {            for(;;)            {                state_data old_state=state;                for(;;)                {                    state_data new_state=old_state;                    if(new_state.exclusive || new_state.exclusive_waiting_blocked || new_state.upgrade)                    {                        ++new_state.shared_waiting;                    }                    else                    {                        ++new_state.shared_count;                        new_state.upgrade=true;                    }                    state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);                    if(current_state==old_state)                    {                        break;                    }                    old_state=current_state;                }                if(!(old_state.exclusive|| old_state.exclusive_waiting_blocked|| old_state.upgrade))                {                    return;                }                                    BOOST_VERIFY(!detail::win32::WaitForSingleObject(unlock_sem,detail::win32::infinite));            }        }        bool try_lock_upgrade()        {            state_data old_state=state;            for(;;)            {                state_data new_state=old_state;                if(new_state.exclusive || new_state.exclusive_waiting_blocked || new_state.upgrade)                {                    return false;                }                else                {                    ++new_state.shared_count;                    new_state.upgrade=true;                }                                state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);                if(current_state==old_state)                {                    break;                }                old_state=current_state;            }            return true;        }        void unlock_upgrade()        {            state_data old_state=state;            for(;;)            {                state_data new_state=old_state;                new_state.upgrade=false;                bool const last_reader=!--new_state.shared_count;                                if(last_reader)                {                    if(new_state.exclusive_waiting)                    {                        --new_state.exclusive_waiting;                        new_state.exclusive_waiting_blocked=false;                    }                    new_state.shared_waiting=0;                }                                state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);                if(current_state==old_state)                {                    if(last_reader)                    {                        release_waiters(old_state);                    }                    break;                }                old_state=current_state;            }        }        void unlock_upgrade_and_lock()        {            state_data old_state=state;            for(;;)            {                state_data new_state=old_state;                bool const last_reader=!--new_state.shared_count;                                if(last_reader)                {                    new_state.upgrade=false;                    new_state.exclusive=true;                }                                state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);                if(current_state==old_state)                {                    if(!last_reader)                    {                        BOOST_VERIFY(!detail::win32::WaitForSingleObject(upgrade_sem,detail::win32::infinite));                    }                    break;                }                old_state=current_state;            }        }        void unlock_and_lock_upgrade()        {            state_data old_state=state;            for(;;)            {                state_data new_state=old_state;                new_state.exclusive=false;                new_state.upgrade=true;                ++new_state.shared_count;                if(new_state.exclusive_waiting)                {                    --new_state.exclusive_waiting;                    new_state.exclusive_waiting_blocked=false;                }                new_state.shared_waiting=0;                state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);                if(current_state==old_state)                {                    break;                }                old_state=current_state;            }            release_waiters(old_state);        }                void unlock_and_lock_shared()        {            state_data old_state=state;            for(;;)            {                state_data new_state=old_state;                new_state.exclusive=false;                ++new_state.shared_count;                if(new_state.exclusive_waiting)                {                    --new_state.exclusive_waiting;                    new_state.exclusive_waiting_blocked=false;                }                new_state.shared_waiting=0;                state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);                if(current_state==old_state)                {                    break;                }                old_state=current_state;            }            release_waiters(old_state);        }                void unlock_upgrade_and_lock_shared()        {            state_data old_state=state;            for(;;)            {                state_data new_state=old_state;                new_state.upgrade=false;                if(new_state.exclusive_waiting)                {                    --new_state.exclusive_waiting;                    new_state.exclusive_waiting_blocked=false;                }                new_state.shared_waiting=0;                state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);                if(current_state==old_state)                {                    break;                }                old_state=current_state;            }            release_waiters(old_state);        }            };}#include <boost/config/abi_suffix.hpp>#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -