草庐IT

pthread_self

全部标签

c++ - 未定义对 `pthread_mutex_trylock' 的引用

我有以下测试程序。#include#includeusingnamespacestd;pthread_mutex_tmymutex=PTHREAD_MUTEX_INITIALIZER;intmain(intargc,char*argv[]){intiret;iret=pthread_mutex_trylock(&mymutex);cout如果我在不添加pthread库的情况下编译它,我会收到pthread_mutex_trylock未解决错误的错误,但仅适用于函数pthread_mutex_trylock。如果我将pthread_mutex_trylock替换为pthread_mute

c++ - pthread - 如何在不调用 join 的情况下开始运行新线程?

我想从主线程开始一个新线程。我不能使用join,因为我不想等待线程退出然后继续执行。基本上我需要的是pthread_start(...)之类的东西,但是找不到。编辑:由于所有答案都建议create_thread应该启动线程,问题是在下面的简单代码中它不起作用。下面程序的输出是“主线程”。似乎子线程从未执行过。知道我哪里错了吗?在Fedora14GCC版本4.5.1上编译和运行void*thread_proc(void*x){printf("subthread.\n");pthread_exit(NULL);}intmain(){pthread_tt1;intres=pthread_cr

c++ - 一个物体可以 self 毁灭吗?

我有一个需要自行销毁的对象。可以吗?例子错了吗?voidPawn::specialMoves(Coordinateconst&from,Coordinateconst&to,intpassant){/*...*/m_board->replace(to,newQueen(m_colour));//replacepawnbyqueen}voidBoard::replace(Coordinateconst&to,Piece*newPiece){deletetile[to.x()][to.y()];tile[to.x()][to.y()]=newPiece;} 最佳

c++ - 等效于 Linux 上的 SetThreadPriority (pthreads)

鉴于以下代码,我想知道在linux中假设pthread甚至使用Boost.ThreadAPI的等效代码是什么。#includeintmain(){SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);return0;} 最佳答案 Linux中的SetThreadPriority等价于pthread_setschedprio(pthread_tthread,intpriority)。查看manpage.编辑:这是等效的示例代码:#includeintmain(){p

c++ - 混合 C++11 std::thread 和 C 系统线程(即 pthreads)

我正在编写一个多线程C++程序并希望使用多线程C库。这个库希望我使用native系统方法为其创建一些工作线程,并使用如下代码将控制权传递给它的run()函数:voidsystem_specific_thread_init();#ifdef_WIN32DWORDWINAPIsystem_specific_thread_run(LPVOIDunused){library_run();return0;}voidsystem_specific_thread_init(){Createthread(NULL,0,system_specific_thread_run,NULL,0,NULL);}#

c++ - pthreads:读/写锁,将读锁升级为写锁

我在Linux上使用读/写锁,我发现尝试将读锁定对象升级为写锁定死锁。即//acquirethereadlockinthread1.pthread_rwlock_rdlock(&lock);//makeadecisiontoupgradethelockinthreads1.pthread_rwlock_wrlock(&lock);//thisdeadlocksasalreadyholdreadlock.我已经阅读了手册页,它非常具体。Thecallingthreadmaydeadlockifatthetimethecallismadeitholdstheread-writelock(w

c++ - 如何在 Qt Creator 上使用 pthread

我想执行下面的代码。#include#includevoidoutput(){std::cout我无法执行它。QtCreator输出在抛出'std::system_error'的实例后调用终止what():不允许操作但是我可以使用-pthread选项在终端上执行。您能告诉我如何在QtCreator中使用-pthread选项吗?我的开发环境是Ubuntu(12.04)、g++4.6.3、QtCreator(2.4.1)。谢谢。 最佳答案 您还需要链接到-pthread。如果您使用g++main.cpp-std=c++0x-pthrea

c++ - 在 g++ 中是在后台使用 pthreads 的 C++ 11 线程模型吗?

我只是在尝试g++4.6和C++11功能。每次我使用-std=c++0x标志编译一个简单的线程代码时,它要么因段错误而崩溃,要么只是抛出一些奇怪的异常。我阅读了一些与C++11线程相关的问题,我意识到,我还需要使用-pthread标志来正确编译代码。使用-pthread工作正常,我能够运行线程代码。我的问题是,C++11多线程模型是否在后台使用Pthreads?还是从头开始写的?我不知道是否有任何成员是gcc贡献者,但我只是好奇。 最佳答案 如果你运行g++-v,它会给你一堆关于它是如何配置的信息。其中之一通常是一条看起来像的线Th

C++ 模板 : Convincing self against code bloat

我听说过C++模板上下文中的代码膨胀。我知道现代C++编译器并非如此。但是,我想构建一个例子并说服自己。假设我们有一个类templateclassArray{public:T*data();private:Telems_[N];};templateT*Array::data(){returnelems_;}此外,假设types.h包含typedefArrayMyArray;x.cpp包含MyArrayArrayX;和y.cpp包含MyArrayArrayY;现在,如何验证MyArray::data()的代码空间对于ArrayX和ArrayY是否相同?我还应该从这个(或其他类似的简单)示

c++ - pthread_once() 中的竞争条件?

我在一个线程中有一个std::future正在等待另一个线程中设置的std::promise。编辑:用一个将永远阻塞的示例应用更新了问题:更新:如果我改用pthread_barrier,下面的代码会not阻塞。我创建了一个测试应用程序来说明这一点:基本上类foo创建一个thread在其运行函数中设置一个promise,并在构造函数中等待该promise被设置。一旦设置,它会增加一个atomic计数然后我创建一堆这些foo对象,将它们拆掉,然后检查我的count。#include#include#include#include#include#includestructfoo{foo(s