草庐IT

thread-sanitizer

全部标签

Android:什么时候应该使用 Handler(),什么时候应该使用 Thread?

当我需要异步运行某些东西时,例如长时间运行的任务或使用网络的逻辑,或出于任何原因,启动新线程并运行它可以正常工作。创建Handler并运行它也可以。有什么不同?我应该什么时候使用每一个?使用Handler而不是Thread的优点/原因是什么?PS。-为了这个问题,让我们忽略AsyncTask。-Handler().postDelayed用例对我来说很清楚,为了这个问题,让我们假设我需要立即开始任务。 最佳答案 如果你正在做的任何事情都是“重”的,那么你应该在一个线程中做它。如果您没有在自己的线程中明确启动它,那么它将在主(UI)线程

Android 基础知识 : running code in the UI thread

从在UI线程中运行代码的观点来看,两者有什么区别:MainActivity.this.runOnUiThread(newRunnable(){publicvoidrun(){Log.d("UIthread","IamtheUIthread");}});或MainActivity.this.myView.post(newRunnable(){publicvoidrun(){Log.d("UIthread","IamtheUIthread");}});和privateclassBackgroundTaskextendsAsyncTask{protectedvoidonPostExecute

c++ - 我想杀死一个 std::thread 使用它的线程对象?

这个问题在这里已经有了答案:关闭9年前.PossibleDuplicate:C++0xthreadinterruption我正在尝试通过使用其线程对象来杀死/停止c++std::thread。我们怎样才能做到这一点? 最佳答案 @bamboon的回答很好,但我觉得这值得更强有力的声明。无论您使用哪种语言,您的程序都会获取和释放资源:内存、文件描述符……对于一次性触发的简单程序,泄漏资源无关紧要:当程序结束时,现代操作系统会自动占用资源返回;但是对于长时间运行的程序,基本要求是不泄露资源,或者至少不重复。因此,您应该从一开始就被教导,

c++ - OpenMP set_num_threads() 不工作

我正在使用C++中的OpenMP编写一个并行程序。我想用omp_set_num_threads()控制程序中的线程数,但是不行。#include#include#include"mpi.h"usingnamespacestd;intmyrank;intgroupsize;doublesum;doublet1,t2;intn=10000000;intmain(intargc,char*argv[]){MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&myrank);MPI_Comm_size(MPI_COMM_WORLD,&group

c++ - 停止 C++ 11 std::threads 等待 std::condition_variable

我正在尝试了解新C++11标准中的基本多线程机制。我能想到的最基本的例子如下:生产者和消费者在不同的线程中实现生产者将一定数量的项目放入队列中消费者从队列中取出元素(如果有的话)这个例子也在许多关于多线程的教科书中使用,关于通信过程的一切都很好。但是,在停止消费者线程时我遇到了问题。我希望消费者一直运行,直到它得到一个明确的停止信号(在大多数情况下,这意味着我等待生产者完成,以便我可以在程序结束之前停止消费者)。不幸的是,C++11线程缺乏中断机制(例如,我从Java中的多线程中知道)。因此,我必须使用像isRunning这样的标志来表示我希望线程停止。现在的主要问题是:在我停止生产者

c++ - thread_local 静态成员模板定义 : initialisation fails with gcc

当C++类中的静态成员同时是thread_local和成员模板时,它不会被初始化。#include#includeclassA{public:templatethread_localstaticstd::unordered_mapm;};templatethread_localstd::unordered_mapA::m{};intmain(){//A::m=std::unordered_map{};//solvestheproblemstd::cout.bucket_count().insert({1,2});//causesSIGPFE(hashmodulobucket_count

c++ - 错误 : BOOST DISABLE THREADS

我的boost库有一些问题。我正在使用freebsd并使用端口安装了我的boost。Boost版本是:1.45,我使用g++47作为编译器。我也从来没有在那儿定义过BOOSTDISABLETHREADS:/usr/local/include/boost/config/user.hpp。我的错误也正是:/usr/local/include/boost/config/requires_threads.hpp:29:4:error:#error"Threadingsupportunavaliable:ithasbeenexplicitlydisabledwithBOOST_DISABLE_T

c++ - std::this_thread::sleep_for() 和 GCC

当我尝试编译这个简单的程序时:#includevoidf(){std::this_thread::sleep_for(std::chrono::seconds(3));}intmain(){std::threadt(f);t.join();}在Ubuntu10.04(32位)上使用gcc版本4.4.3:$g++-std=c++0x-pthreada.cpp-oa我明白了:error:‘sleep_for’isnotamemberof‘std::this_thread’我查看了标题“线程”。sleep_for()受_GLIBCXX_USE_NANOSLEEP保护#ifdef_GLIBCX

c++ - 在 C++11 中通过引用 std::thread 传递对象

为什么在创建std::thread时不能通过引用传递对象?例如,以下代码段给出了编译错误:#include#includeusingnamespacestd;staticvoidSimpleThread(int&a)//compileerror//staticvoidSimpleThread(inta)//OK{cout错误:Infileincludedfrom/usr/include/c++/4.8/thread:39:0,from./std_thread_refs.cpp:5:/usr/include/c++/4.8/functional:Ininstantiationof‘str

c++ - 在 C++11 中, "does not represent a thread of execution"的线程有什么意义?

查看C++11中的新线程以了解它映射到pthread的难易程度,我注意到thread构造函数区域中的奇怪部分:thread();Effects:Constructsathreadobjectthatdoesnotrepresentathreadofexecution.Postcondition:get_id()==id()Throws:Nothing.换句话说,线程的默认构造函数实际上似乎并没有创建线程。显然,它创建了一个线程对象,,但是如果没有支持代码,它到底有什么用呢?是否有其他方式可以将“执行线程”附加到该对象,例如thrd.start()或类似的东西?