1、std::atomic_flag介绍
std::atomic_flag其实是一个原子的布尔类型,先来看下关于std::atomic_flag的定义,如下是摘抄源文件中的定义:
/// atomic_flag
struct atomic_flag : public __atomic_flag_base
{
atomic_flag() noexcept = default;
~atomic_flag() noexcept = default;
atomic_flag(const atomic_flag&) = delete;
atomic_flag& operator=(const atomic_flag&) = delete;
atomic_flag& operator=(const atomic_flag&) volatile = delete;
// Conversion to ATOMIC_FLAG_INIT.
constexpr atomic_flag(bool __i) noexcept
: __atomic_flag_base{ _S_init(__i) }
{ }
_GLIBCXX_ALWAYS_INLINE bool
test_and_set(memory_order __m = memory_order_seq_cst) noexcept
{
return __atomic_test_and_set (&_M_i, __m);
}
_GLIBCXX_ALWAYS_INLINE bool
test_and_set(memory_order __m = memory_order_seq_cst) volatile noexcept
{
return __atomic_test_and_set (&_M_i, __m);
}
_GLIBCXX_ALWAYS_INLINE void
clear(memory_order __m = memory_order_seq_cst) noexcept
{
memory_order __b = __m & __memory_order_mask;
__glibcxx_assert(__b != memory_order_consume);
__glibcxx_assert(__b != memory_order_acquire);
__glibcxx_assert(__b != memory_order_acq_rel);
__atomic_clear (&_M_i, __m);
}
_GLIBCXX_ALWAYS_INLINE void
clear(memory_order __m = memory_order_seq_cst) volatile noexcept
{
memory_order __b = __m & __memory_order_mask;
__glibcxx_assert(__b != memory_order_consume);
__glibcxx_assert(__b != memory_order_acquire);
__glibcxx_assert(__b != memory_order_acq_rel);
__atomic_clear (&_M_i, __m);
}
private:
static constexpr __atomic_flag_data_type
_S_init(bool __i)
{ return __i ? __GCC_ATOMIC_TEST_AND_SET_TRUEVAL : 0; }
};
在以上比较重要的成员函数如下:
test_and_set(memory_order __m = memory_order_seq_cst) noexcept
clear(memory_order __m = memory_order_seq_cst) noexcept
由上面可以看出其支持两种操作,test_and_set和clear。
std::atomic_flag::test_and_set() 作用是检查该对象的标志,如果说std::atomic_flag之前没有被设置过,那么就在调用的地方设置标志,并返回true,否则就返回false。
std::atomic_flag::clear(),作用是清除atomic_flag对象。
2、关于std::atomic_flag使用示例
std::atomic_flag使用示例
/*************************************************************************
> File Name: thread_atomic.cpp
> Author: 小和尚敲木鱼
> Mail:
> Created Time: Mon 20 Sep 2021 11:35:21 PM PDT
************************************************************************/
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
using namespace std;
/*****************************文件说明***********************************
***********************************************************************/
std::atomic_flag g_lock = ATOMIC_FLAG_INIT;
static bool g_Running = false;
static int g_data = 0;
void thread_function()
{
std::cout << "thread_function Running..." << std::endl;
{
while(g_lock.test_and_set());
g_data += 10;
std::cout << "g_data = " << g_data << std::endl;
std::cout << "[" << __func__ << "]" << std::endl;
g_lock.clear();
}
}
int main(int agc,char * agv[])
{
int data = 10;
std::thread thread1(thread_function);
std::cout << "Mian Thread Running..."<< std::endl;
if (thread1.joinable())
thread1.join();
return 0;
}
//OUT
//Mian Thread Running...
//thread_function Running...
//g_data = 10
//[thread_function]
std::atomic封装基本数据的使用示例
std::aotmic搭配bool变量使用
/*************************************************************************
> File Name: thread_atomic.cpp
> Author: 小和尚敲木鱼
> Mail:
> Created Time: Mon 20 Sep 2021 11:35:21 PM PDT
************************************************************************/
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
using namespace std;
/*****************************文件说明***********************************
***********************************************************************/
std::atomic<bool> g_Going(false);
std::atomic_flag g_lock = ATOMIC_FLAG_INIT;
static int g_data = 0;
static bool g_Running = false;//控制子线程整体是否循环运行
void thread_function()
{
std::cout << "thread_function Running..." << std::endl;
while(g_Running)
{
{
while(!g_Going);//如果g_Going没有被设置true,则一直等待。
if (g_lock.test_and_set())
{
g_data += 10;
std::cout << "g_data = " << g_data << std::endl;
std::cout << "[" << __func__ << "]" << std::endl;
g_lock.clear();
g_Going = false;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
std::cout << "thread_function Exit..." << std::endl;
}
int main(int agc,char * agv[])
{
int data = 10;
g_Running = true;
std::thread thread1(thread_function);
std::cout << "Mian Thread Running..."<< std::endl;
for (int i = 0 ; i < 10 ; i++)
{
g_Going = true;//设置g_Going,使能子线程继续往下执行。
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
g_Running = false;
g_Going = true;
if (thread1.joinable())
thread1.join();
return 0;
}
//OUT
//Mian Thread Running...
//thread_function Running...
//g_data = 10
//[thread_function]
//g_data = 20
//[thread_function]
//g_data = 30
//[thread_function]
//g_data = 40
//[thread_function]
//g_data = 50
//[thread_function]
//g_data = 60
//[thread_function]
//g_data = 70
//[thread_function]
//g_data = 80
//[thread_function]
//g_data = 90
//[thread_function]
//g_data = 100
//[thread_function]
//thread_function Exit...
/**************************end of file**********************************/
上述例子中,主线程操作子线程10次对全局变量进行操作,通过g_Going 控制子线程是否继续向下执行。
3、总结
在C11中的std::aotmic原子操作还是比较简单的,主要是配合bool变量进行线程的控制,使用起来还是非常方便。