相关疑难解决方法(0)

指针和引用之间的差异作为线程参数

这是一个例子:

#include<iostream>
#include<thread>
using namespace std;

void f1(double& ret) {
   ret=5.;
}

void f2(double* ret) {
   *ret=5.;
}

int main() {
   double ret=0.;
   thread t1(f1, ret);
   t1.join();
   cout << "ret=" << ret << endl;
   thread t2(f2, &ret);
   t2.join();
   cout << "ret=" << ret << endl;   
}
Run Code Online (Sandbox Code Playgroud)

输出是:

ret=0
ret=5
Run Code Online (Sandbox Code Playgroud)

用gcc 4.5.2编译,有和没有-O2标志.

这是预期的行为吗?

这个节目数据是否免费比赛?

谢谢

c++ multithreading reference c++11 stdthread

38
推荐指数
3
解决办法
2万
查看次数

c ++:错误:'class std :: result_of <void(*(std :: unordered_map)中没有名为'type'的类型

以下是使用两个线程插入哈希表进行测试的简单程序.对于测试,不使用锁定.

#include <iostream>
#include <unordered_map>
#include <thread>

using namespace std;

void thread_add(unordered_map<int, int>& ht, int from, int to)
{
    for(int i = from; i <= to; ++i)
        ht.insert(unordered_map<int, int>::value_type(i, 0));
}

void test()
{
    unordered_map<int, int> ht;
    thread t[2];

    t[0] = thread(thread_add, ht, 0, 9);
    t[1] = thread(thread_add, ht, 10, 19);

    t[0].join();
    t[1].join();

    std::cout << "size: " << ht.size() << std::endl;
}

int main()
{
    test();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,编译时会出错.

$ g++ -std=c++11 -pthread test.cpp
...
/usr/include/c++/4.8.2/functional:1697:61: error: no type …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11

29
推荐指数
2
解决办法
3万
查看次数

std :: thread通过引用调用复制构造函数

我有一个使用std :: thread将数据传递到线程的问题.我以为我理解了复制构造函数等的一般语义,但似乎我并没有完全理解这个问题.我有一个名为Log的简单类,它隐藏了它的复制构造函数:

class Log
{
public:
    Log(const char filename[], const bool outputToConsole = false);
    virtual ~Log(void);

    //modify behavior
    void appendStream(std::ostream *);
    //commit a new message
    void commitStatus(const std::string str);

private:
    //members
    std::ofstream fileStream;
    std::list<std::ostream *> listOfStreams;

    //disable copy constructor and assignment operator
    Log(const Log &);
    Log & operator=(const Log &);
}
Run Code Online (Sandbox Code Playgroud)

现在我主要基于http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp11/echo/blocking_tcp_echo_server.cpp

int main()
{
    static int portNumber = 10000;

    Log logger("ServerLog.txt", true);
    logger.commitStatus("Log Test String");

    try {
        boost::asio::io_service ioService;
        server(ioService, portNumber, logger);
    }
    catch (std::exception &e)
    { …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading copy-constructor c++11 stdthread

28
推荐指数
2
解决办法
2万
查看次数

C++ 引用包装器作为函数参数

根据我的理解,引用包装器只是引用的包装器,没有什么特别的。但是,当作为函数参数传递时,为什么它被视为函数内部的引用本身(而不是包装器)呢?

#include <iostream>
#include <functional>
using namespace std;

void f(int& x){
    cout<<"f on int called"<<endl;
}

void f(reference_wrapper<int>& x){
    cout<<"f on wrapper called"<<endl;
}

int main(){
    int x = 10;
    f(ref(x)); // f on int called, why?

    reference_wrapper<int> z = ref(x);
    f(z); // f on wrapper called, this makes sense though
}

Run Code Online (Sandbox Code Playgroud)

为什么 ref(x) 在函数调用中被视为 x 本身?我遇到这个问题是因为我试图理解在不同线程之间传递数据时 ref() 的使用。我认为 ref() 是必要的,因此任何带有“&”的函数参数都不需要重写以避免线程相互干扰。但是为什么线程可以将 ref(x) 视为 x 而不使用 x.get() 呢?

c++ multithreading reference-wrapper

3
推荐指数
1
解决办法
696
查看次数

std :: threads构造函数参数错误

我不是一个优秀的C++程序员,但目前使用C++的一些功能来清理我的C代码的脏部分.g ++编译器抱怨threads[i] = thread(split, i, sums[i], from, to, f, nThreads);.请帮我找到问题所在.

// mjArray只是一个瘦的类而不是std :: vector,在我的情况下太重了.

#include <cstdio>
#include <cmath>
#include <ctime>
#include <thread>
using namespace std;

template<typename T>
class mjArray {
private:
    T* _array;
    int _length;

public:
    mjArray(int length) {
        _array = new T[length];
        _length = length;
    }

    mjArray(int length, T val) {
        _array = new T[length];
        _length = length;
        for (int i = 0; i < length; ++i) {
            _array[i] = val;
        }
    }

    ~mjArray() {
        delete[] …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading templates move c++11

1
推荐指数
1
解决办法
139
查看次数

为什么我不能使用std :: thread通过引用发送对象

我的代码是这样的: -

#include <iostream>
#include <thread>
using namespace std;
void swapno (int &a, int &b)
{
    int temp=a;
    a=b;
    b=temp;
}
int main()
{
    int x=5, y=7;
    cout << "x = " << x << "\ty = " << y << "\n";
    thread t (swapno, x, y);
    t.join();
    cout << "x = " << x << "\ty = " << y << "\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码无法编译.任何人都可以帮我解释原因吗?不仅如此代码,但是代码也无法发送std::unique_ptr的参考.怎么了std::thread

c++ multithreading reference

1
推荐指数
1
解决办法
131
查看次数

使用带有参数的类成员函数调用std :: thread构造函数

我知道存在这个问题,一个也.我已经完成了他们两个并且无法解决我的问题.我能够为带有零参数的类成员函数运行示例,但是如果成员函数有参数,则无法推断出线程的构造函数会是什么样子.

我有一个A类.A有一个成员函数f,它有2个参数p1和p2.我已经实例化了A类的对象a.我想在一个线程中调用函数f.

来自cppreference.com:

template< class Function, class... Args > 
explicit thread( Function&& f, Args&&... args );
Run Code Online (Sandbox Code Playgroud)

是我必须编写构造函数.

我无法破译上述定义.如何调用af(p1,p2)定义为

void A::f(int p1, int p2)

在线程?

c++ multithreading c++11

0
推荐指数
1
解决办法
466
查看次数