类线程安全的静态const char[]
static const char [] in class thread safe?
static const
's在类线程安全吗?在下面的代码中,我有trailingBytesForUTF8
,这是一个static const
字符数组。可能有许多线程拥有自己的CConvertUTF
类的对象实例。当多个线程同时访问同一个trailingBytesForUTF8
数组时,是否会有任何可变状态问题,或者任何其他线程问题?还要注意,线程将永远不会共享CConvertUTF
类的相同对象实例。
// .h
class CConvertUTF final
{
private:
static const char trailingBytesForUTF8[256];
public:
bool IsLegalUTF8Sequence(const char *source, const char *sourceEnd);
bool IsLegalUTF8(const char *source, int length);
};
// .cpp
const char CConvertUTF::trailingBytesForUTF8[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
};
bool CConvertUTF::IsLegalUTF8Sequence(const char *source, const char *sourceEnd) {
int length = trailingBytesForUTF8[*source]+1;
if (source+length > sourceEnd) {
return false;
}
return IsLegalUTF8(source, length);
}
bool CConvertUTF::IsLegalUTF8(const char *source, int length) {
char a;
const *char = source+length;
switch (length) {
default: return false;
/* Everything else falls through when "true"... */
case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
case 2: if ((a = (*--srcptr)) > 0xBF) return false;
switch (*source) {
/* no fall-through in this inner switch */
case 0xE0: if (a < 0xA0) return false; break;
case 0xED: if (a > 0x9F) return false; break;
case 0xF0: if (a < 0x90) return false; break;
case 0xF4: if (a > 0x8F) return false; break;
default: if (a < 0x80) return false;
}
case 1: if (*source >= 0x80 && *source < 0xC2) return false;
}
if (*source > 0xF4) return false;
return true;
}
只读(const)变量在销毁之前始终是线程安全的。由于静态对象只在程序终止时销毁,因此它们在程序的整个生命周期内都是有益的。
唯一的例外是具有mutable
成员的对象,但这不适用于char
数组。
任何指定为static const
的数据都是全局的,并且是只读的。
这意味着它们不受竞态条件的影响,因为没有人会修改数据。
要出现数据竞争条件,必须至少有一个写操作
相关文章:
- 在字符串函数中抛出'char const*'实例后调用的终止
- 双重标准?为什么只有 char* const&a = "bla" 的警告?
- 包括"lvtocon.h",未定义对'operator<<(std::ostream&, char const*)的引用
- 为什么 const char* const & = 可以编译"hello"?
- 将字符串文本常量定义为 char const* 和 wchar const*
- 如何将std::wstring转换为char const[]
- "char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {"剂量是什么意思
- 使用提升对字符串进行标记化时,将令牌转换为 char* const* 时失败
- 如何从 std::initializer_list<char const* 构建 std::vector<std::string>>
- 将 std::vector<std::string> 转换为 const char* const*
- 引发"char const*"错误的实例后调用的终止
- 将const char * const参数成员分配给新值
- 从'const char**'到'char* const*'的转换无效
- 从 sub_match<常量字符 *>' 转换为'const char *const &'
- char*const和constchar*之间有什么区别?(重复-需要更多澄清)
- 使用工厂方法时编译器错误:无法转换"const std::p air<char* const
- HEVC 解码器端口 Android 警告:从 'signed char*' 到 'char const* 的转换无效*
- 如何从'char const*'中删除常量
- 'char const *str'作为模板参数
- 为什么不;t strlen(.)应为const char*const str,而不是const char*