我有一个功能.在我的函数中有一个c ++线程和一个Qtimer.通过c ++线程我收到ARP回复数据包,并通过QTimer我发送ARP请求数据包.
简化结构:
int foo()
{
... some codes ...
QTimer::singleShot(1000, this, SLOT(beginSending()));
std::thread tCapture(Capture);
tCapture.join();
return 0;
}
void Capture()
{
while ( ! finishCapturing )
{
do sth
}
}
Run Code Online (Sandbox Code Playgroud)
在tCapture线程中我有一个使用所有CPU的while循环并且Qtimer不起作用!
我使用.join()是因为我想等待线程完成.
当我finishCapturing在Qtimer插槽中设置标志时,线程将完成.
上面的代码无法正常工作,因为c ++线程占用了所有CPU!
问题是什么?
非常感谢.雅阿里
#include <thread>
#include <iostream>
using namespace std;
class A
{
public:
A(){}
void m(std::string* s)
{
cout<<*s;
}
void m(std::string s)
{
cout<<s;
}
};
int main()
{
A a;
string str="hi!\n";
std::thread(&A::m,a,&str);
}
Run Code Online (Sandbox Code Playgroud)
这不编译; 它给:
error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, A&, std::string*)’
Run Code Online (Sandbox Code Playgroud)
如果我删除第二个成员,它编译!为什么?我不能在std :: thread中使用重载方法?
我想学习在 VS2012 中使用 C++ 11 std::threads,我写了一个非常简单的 C++ 控制台程序,它有两个线程,它只是增加一个计数器。我还想测试使用两个线程时的性能差异。测试程序如下:
#include <iostream>
#include <thread>
#include <conio.h>
#include <atomic>
std::atomic<long long> sum(0);
//long long sum;
using namespace std;
const int RANGE = 100000000;
void test_without_threds()
{
sum = 0;
for(unsigned int j = 0; j < 2; j++)
for(unsigned int k = 0; k < RANGE; k++)
sum ++ ;
}
void call_from_thread(int tid)
{
for(unsigned int k = 0; k < RANGE; k++)
sum ++ ;
}
void test_with_2_threds()
{
std::thread t[2]; …Run Code Online (Sandbox Code Playgroud) 我有一个后台线程用于上传文件.它循环运行; 它做了一些工作,然后睡觉直到超时过去或直到通过条件变量明确通知有更多的工作要做.问题是有时候我无法让线程快速退出.
这是一个简化版本:
std::thread g_thread;
std::mutex g_mutex;
std::condition_variable g_cond;
bool g_stop = false;
void threadLoop()
{
while (!g_stop)
{
printf("doing some stuff\n");
std::unique_lock<std::mutex> lock(g_mutex);
g_cond.wait_for(lock, std::chrono::seconds(15));
}
}
int main(int argc, char* argv[])
{
g_stop = false;
g_thread = std::thread(threadLoop);
printf("hello\n");
g_stop = true;
g_cond.notify_one();
g_thread.join();
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个测试程序时,我希望它能够快速退出,但有时它会卡在wait_for()中.我想也许在线程在wait_for()中休眠之前发生了notify_one(),但是在检查了g_stop之后.
是否有一个简单的解决方案,或其他更好的设计模式?
根据输出信息,任何人都可以解释下面的代码?
怎么可能都是(a==1 && a==2 && a==3)真的?
#include <iostream>
#include <thread>
int a = 0;
int main()
{
std::thread runThread([]() { while (true) { a = 1; a = 2; a = 3; }});
while (true)
{
if (a == 1 && a == 2 && a == 3)
{
std::cout << "Hell World!" << std::endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Run Code Online (Sandbox Code Playgroud)Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell …
我正在尝试理解 中的一些基本概念std::thread,但我对此仍然没有信心。根本问题是:
当我超过 的值时实际发生了什么std::thread::hardware_concurrency(),就像我在下面所做的那样?
我知道该方法只是一个提示,但在这种情况下 8 应该是准确的。我没有看到任何警告或错误,那么到底发生了什么?
join()我怀疑这与我对和缺乏理解有关detach(),这让我想到了第二个问题。
join()我知道如果我在没有 a或 a 的情况下启动线程detach(),我将收到运行时错误。据我从阅读和观察中了解到,join()导致线程阻塞直到完成执行,而detach()基本上相反,允许线程疯狂运行直到完成,如果该线程不终止,则可能会打开一罐蠕虫它自己的。
join()根据我的观察,和的用法似乎detach()是相互排斥的。这是错误的吗?为什么我需要在同一个线程上使用join()and ?detach()
就我的第一个问题而言,我什至无法开始猜测。我预计会出现某种类型的运行时错误,或者一些更明显的强制阻塞。
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <thread>
#include <vector>
std::vector<unsigned> getNumbers(unsigned n)
{
std::vector<unsigned> out;
while(n > 0)
{
out.push_back(rand());
n--;
}
return out;
}
int main() {
srand(time(nullptr));
std::vector<std::thread> threads;
unsigned maxThreads = std::thread::hardware_concurrency();
std::cout << "Max threads: " << maxThreads << std::endl; …Run Code Online (Sandbox Code Playgroud) 我只是在学习如何std::thread在c ++ 11中使用。基本上,我使用的硬件中有很长的数据列表(想象一下0-15000之间的for循环)和1568线程。我想要一个单独的线程来处理每个样本。我了解如何创建第一个1568线程,它工作正常。但是一旦到达N_thread + 1示例,我便要检查是否有可用线程。如果存在,则将该数据样本发送到该线程。每个线程都发送到互斥锁功能,该功能最后会解锁。也许我误解了线程是如何工作的,不能以这种方式做事?也许有更好的线程/ CPU分配库可以提供帮助?
就像我说的那样,我可以说创建了1568个线程,然后运行并加入它们,最终结果很好。只需要更多信息。
这是我的主要
int main(){
cout<<"In main"<<endl;
CSVReaderUpdatedStructure reader("data.csv");
vector<STMDataPacket> DataList = reader.GetData();
thread_pool Pool(THREAD_COUNT);
auto startT0 = chrono::high_resolution_clock::now();
for(unsigned s=0; s<DataList.size()-1; s++){
cout<<"analysing sample "<<s<<endl;
auto done = Pool.add_task([s= s, Sample= DataList[s], t_inf = time_info,wf=writefile, f=factor]{GetDMWPulses(s, Sample, t_inf, wf,f);});
done.wait();
}
auto stop = chrono::high_resolution_clock::now();
cout<<"pulses "<<pulses.size()<<endl;
auto duration = chrono::duration_cast<chrono::microseconds>(stop - startT0);
cout <<"time for MWD full process = "<< duration.count() <<" microseconds "<< endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我编写了以下函数,它接受一个对象 ( element),在现有向量 ( elements) 中为它腾出空间,并通过添加新对象来更新向量:
void addElement(const ElementType& element) {
if (numElements == elements.size()) {
elements.resize(boost::extents[numElements+1]);
}
elements[numElements] = element;
numElements++;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使它对 MPI 线程安全?据我了解,每个线程都知道的大小elements是多少,因此我不明白为什么这个函数不是线程安全的。numElements在此函数外初始化为零,并且是elements向量的大小。
编辑:我使用上面写的函数和 mtx 锁定和解锁如下,但最终elements向量仍然只包含来自第一级的数据。
#pragma omp parallel for collapse(3) schedule(static)
for (long n0 = mgr->startN0; n0 < mgr->startN0 + mgr->localN0; n0++) {
for (int n1 = 0; n1 < ?; n1++) {
for (int n2 = 0; n2 < ?; n2++) {
ElementType element;
std::mutex …Run Code Online (Sandbox Code Playgroud) std::this_thread::sleep_for() 或 usleep()
在 main() 中使用什么会更好?这可能很愚蠢,但我是 C++11 功能的新手。
std::thread我在 Bjarne Stroustrup 的《C++ 编程语言》中看到了一个基本用法的示例,它让我感到困惑。这是示例:
void run(int i, int n) // warning: really poor code
{
thread t1 {f};
thread t2;
vector<Foo> v;
// ...
if (i<n)
{
thread t3 {g};
// ...
t2 = move(t3); // move t3 to outer scope
}
v[i] = Foo{}; // might throw
// ...
t1.join();
t2.join();
}
Run Code Online (Sandbox Code Playgroud)
正如斯特鲁斯特鲁普所写:
我们可能永远不会到达
join()最后的 2。在这种情况下,析构函数t1将终止程序。
但是,在什么情况下t1调用 的析构函数呢?线程t1不会超出其范围。也没有明确的delete调用。
我尝试通过适当的更改来运行此代码,但仍然找不到如何t1调用 的析构函数。