C++ Mac OS X pthread Initialization
C++ Mac OS X pthread Initialization
我在Mac OS X 10.12上遇到了一个错误,涉及Clang,它不想编译我的代码。我希望它通过编译器编译,但它坚持我"错误:成员初始化器'pthread_mutex_init'不命名非静态数据成员或基类"时,调用pthread_mutex_init。我已经尝试在pthread_mutex_t声明前添加和删除"静态",我已经包含了我的pthread头文件
编辑:是的,我在我的file.cpp文件中包含了file.h。编辑#2:我试过mutex_ = (mutex_pthread_t)PTHREAD_MUTEX_INITIALIZER,它给了我一些奇怪的错误,告诉我在某处插入"{"。 下面是我的代码:Memory.h:
#include <pthread.h>
class Memory {
static pthread_mutex_t mutex_;
}
Memory.cpp:
#include <Memory.h>
#include <pthread.h>
Memory::Memory() :
#ifdef __APPLE__
pthread_mutex_init(&mutex_, NULL);
#endif
我假设你想在所有内存对象之间共享一个互斥锁?
这里有两种(许多)方法,副作用略有不同:
#include <pthread.h>
class Memory {
// I am assuming that you wanted the mutex to be initialised
// at program start?
static bool init_mutex();
static pthread_mutex_t mutex_;
static bool initialised;
};
bool Memory::initialised = init_mutex();
bool Memory::init_mutex()
{
pthread_mutex_init(&mutex_, 0);
return true;
}
// or what about upon first use of a Memory?
class Memory2
{
struct impl {
impl() {
pthread_mutex_init(&mutex_, 0);
}
pthread_mutex_t mutex_;
};
static impl& get_impl()
{
static impl impl_;
return impl_;
}
Memory2()
{
get_impl();
}
};
相关文章:
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- 在 c++11 中为 pthread 设置调度参数
- 使用 libgit2 创建 NOT INITIAL 提交
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- pthread只有在线程数量较少时才可以正常工作
- pthread执行时间比顺序执行时间差
- 使用 -pthread 如何不违反 ODR 规则?
- 使用 pthread 的多线程
- 销毁 pthread 互斥体和 C++ 中的取消初始化顺序
- 函数 AfxEndThread 中的读取访问冲突"pThread-> was nullptr"
- 锁定步进pthread互斥
- Pthread段错误,使用指向main中变量的指针
- pthread导致的内存泄漏
- GCC:--静态链接到pthread的整个存档配方在最近的GCC版本中停止工作
- 正确地编写一个类,并将pthread与vlc库和c++一起使用
- 直接读取 pthread 互斥锁的所有者字段是否安全?
- 为什么 pthread 会减慢代码速度?
- 对 pthread CLion 的未定义引用
- C++ 17:pthread的生成文件标志
- 将带有结构的参数传递给 pthread