我正在尝试创建一个运行类函数的线程.我认为我做错事的地方靠近我所在的主要地方RowChecker r0(puz, 0, s); thread rt0 {r0.check()};.我的编译器(g ++)告诉我,no matching function for call to ‘std::thread::thread(<brace-enclosed initializer list>)’
所以我理解我没有正确地进行调用.格式化此调用以创建新线程的正确方法是什么?
#include <iostream>
#include <thread>
using namespace std;
class Sum
{
private:
int sum;
public:
Sum();
int getSum();
void addSum();
};
class RowChecker
{
private:
int puz[9][9];
int myRow;
Sum* sum;
public:
RowChecker(int puzzel[9][9], int row, Sum* shared);
void check();
};
Sum::Sum() {
sum = 0;
}
int Sum::getSum() {
return sum;
}
void Sum::addSum() {
++sum;
}
RowChecker::RowChecker(int puzzel[9][9], int …Run Code Online (Sandbox Code Playgroud) 我的线程程序有问题.我知道问题是什么,我只是不知道如何解决它.我正在设置一个任意数量的线程来创建一个mandelbrot集,然后将其写入ppm文件.我正在使用std :: thread的向量并调用Mandelbrot类成员函数来执行线程.问题出在这里.我正在调用编译器不喜欢的void(void)函数.我如何解决这个问题,以便线程执行void(void)函数?我的代码如下:
int main(int argc, char **argv) {
const unsigned int WIDTH = 1366;
const unsigned int HEIGHT = 768;
int numThreads = 2;
Mandelbrot mandelbrot(WIDTH, HEIGHT);
if(argc > 1) {
numThreads = atoi(argv[1]);
}
std::vector<std::thread> threads;
for(int i = 0; i < numThreads; ++i) {
threads.emplace_back(mandelbrot.mandelbrotsetThreaded());
}
for(int i = 0; i < numThreads; ++i) {
threads[i].join();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
void Mandelbrot::mandelbrotsetThreaded() {
while(true) {
int row = 0;
{
std::lock_guard<std::mutex> lock(row_mutex);
row = cur_row++; …Run Code Online (Sandbox Code Playgroud) 当我这样使用时std::thread:
func()
{
std::thread(std::bind(&foo, this));
}
Run Code Online (Sandbox Code Playgroud)
线程对象在堆栈中分配,并在func()返回时被销毁.所以我尝试使用它:
func()
{
std::thread* threadPtr = new std::thread(std::bind(&foo, this));
}
Run Code Online (Sandbox Code Playgroud)
我应该在哪儿delete threadPtr?我怎样才能创建最初暂停的线程?
根据我在C++在线文档中收集的内容,分配给连接的std :: thread对象应该调用它的析构函数并表示合法的操作.是这样的吗?
这里有一些例子来说明我的意思:
#include <thread>
#include <vector>
using namespace std;
int main()
{
vector<thread> tvec;
for(int = 0; i < 3; ++i)
{
tvec.push_back(thread(foo));
}
for(size_t i = 0; i < 3; ++i)
{
tvec[i].join();
tvec[i] = thread(foo); // is this ok?
}
for(auto& t : tvec)
{
t.join();
}
}
Run Code Online (Sandbox Code Playgroud) 为什么不在这个抽象基类中创建这样的线程呢?我试图抽象出从这个基类派生的用户的所有多线程细节.当我清楚地写出callbackSquare返回类型时,我不明白它为什么说"没有类型命名为'type'" int.
#include <iostream>
#include <future>
#include <vector>
class ABC{
public:
std::vector<std::future<int> > m_results;
ABC(){};
~ABC(){};
virtual int callbackSquare(int& a) = 0;
void doStuffWithCallBack();
};
void ABC::doStuffWithCallBack(){
for(int i = 0; i < 10; ++i)
m_results.push_back(std::async(&ABC::callbackSquare, this, i));
for(int j = 0; j < 10; ++j)
std::cout << m_results[j].get() << "\n";
}
class Derived : public ABC {
Derived() : ABC() {};
~Derived(){};
int callbackSquare(int& a) {return a * a;};
};
int main(int argc, char **argv)
{ …Run Code Online (Sandbox Code Playgroud) 在std::thread:
为什么?这种API有什么用?
它是线程概念的基础.
堆栈大小,为什么我们不关心内存?也许作者只假设Linux和Windows具有分页内存和64位地址空间,但是没有分页内存的平台呢?
优先事项,如何使任何系统具有可预测的时间而没有优先级?
我的代码如下所示:
#include <list>
#include <thread>
void my_function(int val) {
// Empty function
}
int main() {
std::list<std::thread> threads;
for (int i = 0 ; i < 10 ; i++) {
threads.push_back(std::thread(my_function, i));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用的事实threads.push_back()意味着我运行了复制构造函数std::thread::thread(const thread&).
安全吗?
我应该用std::move吗?
请假设我事先并不知道我需要多少线程,所以用数组或者用一个替换列表std::vector对我来说不是一个选项(std::vector只有当我知道线程的数量时才会有一个选项)前进,因为我买不起矢量的realloc操作).
我正在尝试实现一个API,让用户可以并行创建两个通信通道.一个通道使用TCP,另一个使用UDP.我有两个代表两个频道的课程.这些类实现了不同的功能.我希望两个通道的功能并行运行.为此,我std::thread用来创建两个线程,每个通道一个(类).想法是以下以下头文件的样子
class Channel_1
{
public:
int myfunc(int a, int b);
};
class Channel_2
{
public:
int anotherfunc(int a, int b);
};
Run Code Online (Sandbox Code Playgroud)
在主cpp文件中包含头文件
int main()
{
int a = 10, b = 20;
Channel_1 ch1;
Channel_2 ch2;
std::thread t(ch1.myfunc, a,b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到错误说没有构造函数的实例std::thread存在.
我有以下问题.
以下程序无法编译 g++ -std=c++11 -Wall -Werror test.cpp -o test.o:
#include <thread>
using namespace std;
void fill(int n) {
return;
}
int main() {
thread test(fill, 5);
}
Run Code Online (Sandbox Code Playgroud)
test.cpp:9:12: error: no matching constructor for initialization of 'std::__1::thread'
thread test(fill, 5);
^ ~~~~~~~
Run Code Online (Sandbox Code Playgroud)
难道是因为fill与发生冲突std::fill的#include <algorithm>?我没有把它包括在内,但我想<thread>可能会这样.
将我的函数名称更改为fillie(或其他任何东西)允许它在没有链接的情况下正确编译pthread.
我问,因为它是一个奇怪的编译器错误消息,并且它也意味着线程构造函数不能消除我根据参数使用哪个函数(哪种有意义,但想要确认).
I am confused as to why I am getting a segmentation fault when creating and firing off threads here. It happens in the t[j] = thread(getMax, A); line and I am very confused as to why this is happening. threadMax[] is the max of each thread. getMax() returns the maximum value of an array.
#include <iostream>
#include <stdlib.h>
#include <sys/time.h>
#include <thread>
#define size 10
#define numThreads 10
using namespace std;
int threadMax[numThreads] = {0};
int num =0;
void getMax(double …Run Code Online (Sandbox Code Playgroud)