草庐IT

pthread_mutex_lock

全部标签

c++ - std::unique_lock 和 std::condition_variable 如何工作

我需要弄清楚lock和condition_variable是如何工作的。在此处的-稍微修改过的代码中cplusplusreferencestd::mutexm;std::condition_variablecv;std::stringdata;boolready=false;boolprocessed=false;voidworker_thread(){//Waituntilmain()sendsdatastd::unique_locklk(m);cv.wait(lk,[]{returnready;});//afterthewait,weownthelock.std::coutlk(m

c++ - 无法在 pthread_create 函数中将 '*void(MyClass::*)(void*) 转换为 void*(*)(void*)

我正在尝试使用“CameraManager”类创建一个新线程,但出现以下错误:cannotconvert'*void(CameraManager::*)(void*)tovoid*(*)(void*)inpthread_createfunction我在cameramanager.h文件中定义:public:void*dequeueLoop(void*ptr);在cameramanager.cpp中voidCameraManager::startDequeuing(){dequeuing=true;dequeueThreadId=pthread_create(&dequeueThread

c++ - std::lock_guard 导致未定义的行为

编辑:看起来,问题是我实际上并没有创建一个lock_guard的本地实例,而只是一个匿名的临时实例,它立即再次被销毁,如下面的评论所指出的。Edit2:启用clang的线程清理器有助于在运行时查明这些类型的问题。它可以通过启用clang++-std=c++14-stdlib=libc++-fsanitize=thread*.cpp-pthread这在某种程度上可能是一个重复的问题,但我找不到任何东西,所以如果它真的是重复的,我很抱歉。无论如何,这应该是一个初学者问题。我正在玩一个简单的“Counter”类,比如在文件中内联计数器.hpp:#ifndefCLASS_COUNTER_HPP

c++ - 如何在 C++ 中定义 vector <boost::mutex>?

我想用boost::mutex定义一个vector,例如:boost::mutexmyMutex;std::vectormutexVec;mutexVec.push_back(myMutex);但是,我在Linux上遇到错误:/boost_1_45_0v/include/boost/thread/pthread/mutex.hpp:33:错误:“boost::mutex::mutex(constboost::mutex&)”是私有(private)的/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/

c++ - pthread_create 段错误

我在我的程序中使用“pthread_create”方法,并在该方法中出现段错误。什么可能导致这个?我正在使用正确的参数类型调用此函数!这是代码:pthread_t*_daemon;void*writer(void*arg){//stuffthatdontinvolve"arg"...}intinitdevice(){if(pthread_create(_daemon,NULL,&writer,NULL)!=0)//seginthisline{cerr注意:在调用pthread_create中的writer之前,我也尝试在没有“&”的情况下运行它,而且-我们尝试向该方法发送一些void*

c++ - std::lock_guard 有什么问题

我有简单的代码:第一个线程将std::strings推送到std::list,第二个线程弹出std::strings从这个std::list。所有std::list的操作都受到std::mutexm的保护。此代码将错误永久打印到控制台:"Error:lst.begin()==lst.end()"。如果我将std::lock_guard替换为构造m.lock()和m.unlock()代码将开始正常工作。std::lock_guard有什么问题?#include#include#include#include#includestd::mutexm;std::listlst;voidf2()

c++ - 一旦 std :thread makes into C++Ox,pthreads 会过时吗

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭5年前。Improvethisquestion显然我们仍会维护它,但一旦C++标准保证可用,它会有多大用处。随着新标准的出现,同步原语(Mutex、条件变量)会怎样?您是否认为pthread比std::thread更难掌握?

c++ - boost scoped_lock 互斥锁崩溃

我已经保护了一个std::queue的访问函数,push、pop、size,在这些函数中使用boost::mutexes和boost::mutex::scoped_lock有时它会在作用域锁中崩溃调用栈是这样的:00x0040f005boost::detail::win32::interlocked_bit_test_and_setinclude/boost/thread/win32/thread_primitives.hpp36110x0040e879boost::detail::basic_timed_mutex::timed_lockinclude/boost/thread/wi

c++ - 如何用 "-pthread"而不是 "-mthread"编译 boost_thread?

我有一个操作系统,编译时没有可用的-mthread。我有-pthread。如何用-pthread而不是-mthread编译boost_thread?我当前的编译器构建日志:./b2-j1--with-threadlink=static--prefix=./install-dirreleasethreading=multi--builddir=./build-dirinstallComponentconfiguration:-chrono:notbuilding-context:notbuilding-date_time:notbuilding-exception:notbuilding

c++ - pthread_key_t 与局部变量

我正在使用Pthread在C++中开发一个多线程程序,我需要在每个线程中分配本地内存。谷歌搜索后,我发现pthread_key_t类型是某种映射,允许您在TLS中分配内存。所以我的问题是线程函数中的局部变量和pthread_key_t有什么区别?你能给出一个pthread_key_t的正确用法示例吗? 最佳答案 线程局部存储和局部变量之间的区别在于线程局部存储不需要是函数的局部变量。一旦声明它们的函数返回,常规局部变量可能不再被访问。使用pthread_getspecific和pthread_setspecific访问的线程特定存储