jjzjj

pThreads

全部标签

c++ - std::thread vs pthread,我做错了什么?

我正在从事一个需要在docker容器内执行某些进程的项目。我想处理进程没有按时终止的情况(比如说在10秒内)。我正在使用这个DockerClientpp用于管理基本上只向Docker套接字发出HTTP请求的容器的库。到目前为止一切都很好。要停止花费太长时间的容器,我使用了一个单独的线程。问题是我能够使用ptheads来实现它,但是我找不到使用std::thread和lambas的方法这是我使用pthread的工作实现void*ContainerManager::spawnKiller(void*ref){ContainerManager*self=(ContainerManager*)

c++ - 这个简单的 pthread 代码的奇怪行为

如果我编译并运行下面的代码1#include2#include3#include4#include56#defineNTHREADS47#defineN1008#defineMEGEXTRA1000000910usingnamespacestd;1112pthread_attr_tattr;1314void*doWork(void*threadid){15doubleA[N][N];16inttid=*(reinterpret_cast(threadid));17size_tmyStackSize;18pthread_attr_getstacksize(&attr,&myStackSi

c++ - 测量互斥量争用/解释互斥量输出

我正在尝试使用mutrace测量我的程序中的互斥锁争用,但我对如何使用它输出的报告感到困惑。mutrace:Showingstatisticsforprocesstest(PID:5924).mutrace:1223mutexesused.Mutex#362(0x0x1a23750)firstreferencedby:/usr/local/lib/libmutrace.so(pthread_mutex_init+0x1b2)[0x7fb6b14129b4]./test()[0x500042]Mutex#390(0x0x1a23ac0)firstreferencedby:/usr/loc

c++ - 什么时候调用 CloseHandle?

我有一个主线程接收需要一些时间的Action。所以我创建了一个线程并将工作委托(delegate)给它。主线程在接收作业时调用此执行函数。每个作业都执行此执行。Return_typeexecute(Arguments_here){//ifafilereadcaseDWORDthreadId;HANDLEhThread=CreateThread(NULL,//defaultsecurityattributes0,//usedefaultstacksizeMyAsyncFileRead,//threadfunctionnamedetails,//argumenttothreadfuncti

c++ - pthread_mutex_lock 上的段错误

当我尝试这样做时出现段错误pthread_mutex_lock(&_mutex).这真的很奇怪,我不确定是什么原因造成的。我已经在构造函数中初始化了_mutexpthread_mutex_init(&_mutex,NULL).有什么我能做的吗? 最佳答案 解决了,我对此很恼火。我想将Producer*作为参数发送给Pthread运行的函数,所以我使用了&(*iter),其中iter是在生产者vector上运行的迭代器。我几乎没有注意到它(理所当然地)是一个vector,这意味着我一直在发送Producer**,它产生了未定义的结果。

c++ - 查找线程错误

尝试在编译时查找线程问题有什么意义吗?我说的是数据竞争、死锁、损坏的状态等...... 最佳答案 虽然这不是编译时间,但您可能需要查看Helgrind:OverviewHelgrindisaValgrindtoolfordetectingsynchronisationerrorsinC,C++andFortranprogramsthatusethePOSIXpthreadsthreadingprimitives.ThemainabstractionsinPOSIXpthreadsare:asetofthreadssharingaco

c++ - "thread-local storage not supported for this target",适合#ifdef?

由于每个编译器都有自己的线程本地存储版本,我最终为它创建了一个宏。现在唯一的问题是GCC(关闭了pthreads),这给了我:“此目标不支持线程本地存储”很公平,因为在这种情况下pthreads实际上是关闭的。问题是,是否有一种通用的方法可以使用一些宏来检测这一点,例如#ifdef__GCC_XXX_NO_THREADS_XXX?编辑:请参阅下面接受的答案。另外,这是我的懒惰解决方案:$touchtest.c$gcc-E-dMtest.c>out.1$gcc-pthread-E-dMtest.c>out.2$diffout.*28a29>#define_REENTRANT1这是在Mac

c++ - 为什么我需要从主线程使用 `pthread_exit()`,而它不是由 `pthread_create` 创建的?

我对一些正在测试以开始理解posix线程的代码有疑问。我有这个基本代码:#include#include#include#includeusingnamespacestd;void*printInfo(void*thid){longtid;tid=(long)thid;printf("Hellofromthread%ld.\n",tid);pthread_exit(NULL);}intmain(intargc,charconst*argv[]){intnum=8;pthread_tthreadlist[num];intrc;longt;for(t=0;t非常简单的代码,启动线程并从中打

c++ - 如何在类中使用 pthread_mutex 及其函数?

我已经搜索了很多小时来寻找解决方案,但找不到简单的答案。我有一个类,它使用pthreads。实际的函数指针在类中是静态的,我需要锁定互斥体,因为到目前为止我得到了“奇怪”的结果(参数未正确传递)。然而,pthread_mutex_lock和unlock不会在给定线程的函数内工作,因为它在静态成员函数中,但我不能拥有非静态函数,因为它不会在类内部工作,而且我无法移动它在类之外,因为它将无法访问所需的信息。下面的代码应该解释一下:classFight{pthread_mutex_tthread_mutex;staticvoid*thread_run_fighter(void*temp);p

c++ - P线程锁定

我已经创建了这样的MutexCondition类/*MutexCondtion.hfile*/#ifndefMUTEXCONDITION_H_#defineMUTEXCONDITION_H_#include#includeclassMutexCondition{private:boolinit();booldestroy();protected:pthread_mutex_tm_mut;pthread_cond_tm_con;public:MutexCondition(){init();}virtual~MutexCondition(){destroy();}boollock();bo