标签: stdthread

如何将可变参数参数传递给 std::thread?

我想通过包装 C++11 中的 std::thread 类来使用我自己的 Thread 实现,这样我就可以像我想要的那样处理异常。

这是我的包装类:

#include <Types.hpp>
#include <thread>
#include <exception>
#include <functional>

class Thread
{
    private:

        std::exception_ptr exceptionPtr;
        std::thread thread;

    public:

        using Id = std::thread::id;

        using NativeHandleType = std::thread::native_handle_type;

        Thread() noexcept = default;
        Thread(Thread &&t) noexcept :
            exceptionPtr(std::move(t.exceptionPtr)),
            thread(std::move(t.thread))
        {
        }

        Thread &operator =(Thread &&t) noexcept
        {
            exceptionPtr = std::move(t.exceptionPtr);
            thread = std::move(t.thread);
            return *this;
        }

        template<typename Callable, typename... Args>
        Thread(Callable &&f, Args &&... args) :
            exceptionPtr(nullptr),
            thread([&](Callable &&f, Args &&... args)
            {
                try
                {
                    std::once_flag …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading templates c++11 stdthread

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

从 std::thread::id 结构中检索 Windows 线程标识符

std::thread::id我可以在代码中访问 a ,并且需要使用一些接收参数 ThreadId as DWORD(与返回的相同GetCurrentThreadId())的本机函数。

我找不到任何方法来转换std::thread::id为 Win32 DWORDThreadId。我能找到的最接近的是std::thread有一个native_handle。但我仍然无法创建 来从 astd::thread获取native_handlestd::thread::id,所以我离我需要的还太远。

我有什么遗漏的吗?或者标准可移植线程函数和本机函数之间的差距是否太大以至于标准 api 无法用于我的目的?

c++ multithreading c++11 stdthread

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

C++11 线程与异步

考虑以下我试图启动 10000 个线程的两个代码片段:

片段 1

    std::array<std::future<void>, 10000> furArr_;
    try
    {
        size_t index = 0;
        for (auto & fut : furArr_)
        {
            std::cout << "Created thread # " << index++ << std::endl;
            fut = std::async(std::launch::async, fun);
        }
    }
    catch (std::system_error & ex)
    {
        std::string str = ex.what();
        std::cout << "Caught : " << str.c_str() << std::endl;
    }
    // I will call get afterwards, still 10000 threads should be active by now assuming "fun" is time consuming
Run Code Online (Sandbox Code Playgroud)

片段 2

    std::array<std::thread, 10000> threadArr; …
Run Code Online (Sandbox Code Playgroud)

multithreading c++11 stdasync stdthread

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

在C++ 11智能指针中存储std :: thread

C++ 11上面直接存储作为类成员的优点或缺点std::thread是这样的:

std::thread my_thread;
Run Code Online (Sandbox Code Playgroud)

而不是像这样存储一个std::shared_ptrstd::unique_ptr多个线程:

std::shared_ptr<std::thread> my_thread_ptr;
Run Code Online (Sandbox Code Playgroud)

任何代码选项都比其他更好吗?或者没关系,只需要2种不同的方式来处理线程对象.

c++ shared-ptr c++11 stdthread c++14

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

C++ lambda 函数调用纯虚函数

我正在尝试为 std::thread 创建一个包装类。这个类提供了一个 kick 方法来启动线程并调用一个纯虚函数。我使用派生类来调用这个kick方法,派生类也实现了虚函数。

class Executor
{
public:
    // constructor
    Executor();

    // destructor
    ~Executor();

    // kick thread execution
    void Kick();

private:
    // thread execution function
    virtual void StartExecution() = 0;

    // thread handle
    std::thread mThreadHandle;
};
Run Code Online (Sandbox Code Playgroud)

下面是executor类的实现

Executor::Executor()
{
    // Nothing to be done here
}

Executor::~Executor()
{
    if (mThreadHandle.joinable())
        mThreadHandle.join();
}

void Executor::Kick()
{
    // mThreadHandle = std::thread(&Executor::StartExecution, this);
    mThreadHandle = std::thread([this] {this->StartExecution();});
}
Run Code Online (Sandbox Code Playgroud)

我正在使用继承这个类并实现 StartExecution 方法的Consumer类。当我使用 kick 方法时,它显示调用的纯虚函数并且程序终止。

std::unique_ptr<Consumer> consumer = std::make_unique<Consumer>();
consumer->Kick();
Run Code Online (Sandbox Code Playgroud)

在 executor …

c++ lambda multithreading c++11 stdthread

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

在 pthread_cancel 之后“终止调用而没有活动异常”

在探讨这个问题的条件时,出现了一个问题,以下面的代码为例。

#include <iostream>
#include <thread>
#include <chrono>
#include <stdexcept>
#include <cxxabi.h>

using namespace std;

// mocking external library call stuck in a strictly user-land infinite loop
int buggy_function_simulation()
{
    // cout << "In buggy function" << endl; // (1)
    int counter = 0;
    while (true)
    {
        if ( ++counter == 1000000 ) { counter = 0; }
    }
    return 0;
}

int main(int argc, char **argv) {
    cout << "Hello, world!" << endl;

    auto lambda = []() { …
Run Code Online (Sandbox Code Playgroud)

c++ g++ pthreads c++11 stdthread

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

为什么 std::this_thread::get_id() 函数在 CentOS 中返回相同的数字?

在以下代码中,.get_id()调用在 CentOS 上运行时返回相同的值;但在 Windows 上,相同的代码返回不同的值。

为什么?

#include <unistd.h>
#include <iostream>
#include <thread>

void dosomework()
{
    std::cout << std::this_thread::get_id() << std::endl;
}

int main()
{
    for (int i = 0; i < 10; ++i){
        std::thread connectthread([](){
            dosomework();
        });
        std::cout << "connectthread:" << connectthread.get_id() << std::endl;
        connectthread.join(); 
        sleep(1000);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ stdthread

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

这个简单的多线程代码如何导致内存损坏?

以下代码可靠地产生段错误。

#include <vector>
#include <thread>

class Class {
public:
    Class(const int integer)
        : cinteger(integer), carray(std::array<int, 1>{0})
    {}

   const int operator()() 
   {
    carray[cinteger] = 0;
        return cinteger;
    }
    
private:
    int cinteger;
    std::array<int, 1> carray;
};

class Worker{
    public:
        Worker( Class iClass): 
            wClass(iClass),
            thread(&Worker::thread_main, this)
            {}
        
        void join(){thread.join();}     

    private:
        Class wClass;
        std::thread thread;
        void thread_main(){
            for(int i = 0; i < 50000; i++)
            wClass();
        }
};

int main()
{
    std::vector<Worker> Workers;
    for(int i = 0; i < 4; i++)
        Workers.emplace_back(Class(0));
    for(int i …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11 stdthread

5
推荐指数
2
解决办法
147
查看次数

debian和std :: thread c ++的即时分段错误

得到这个问题 - 在标题..

我有这个代码:

#include <thread>
#include <iostream>

void my_thread_func()
{
    std::cout<<"hello"<<std::endl;
}

int main()
{
    std::thread t(my_thread_func);
    t.join();
}
Run Code Online (Sandbox Code Playgroud)

从网站的某个地方.编译器选项-pthread -std = gnu ++ 0x(也​​试过-std = c ++ 0x)我有段错误.一切都在vmBox上的Debian上.我之前已经启动了其他代码,并且他们工作了.突然间,我在所有正在运行的应用程序中使用std :: thread进行了线程段错误.

编辑:这是来自gdb:

(gdb) where
#0  0x00000000 in ?? ()
#1  0x08048dc9 in thread<void (*)()> (this=0xbffff3fc, 
    __f=0x8048b9f <my_thread_func()>) at /usr/include/c++/4.4/thread:129
#2  0x08048bea in main () at ../test.cpp:18
Run Code Online (Sandbox Code Playgroud)

(当我用std :: thread t(&ClassName :: my_thread_func,ptr)启动更高级的应用程序时,错误是相同的,但是其他行[thread:133])

我正在网上搜索,但我没有找到合适的东西.

c++ debian multithreading stdthread

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

std :: async中的超时

有没有办法在std :: async方法中实现超时,所以如果线程没有在指定的时间内完成,我希望这个调用超时并完成.我该如何实现此功能.

c++ std c++11 stdasync stdthread

4
推荐指数
2
解决办法
4462
查看次数

标签 统计

stdthread ×10

c++ ×9

c++11 ×8

multithreading ×6

stdasync ×2

c++14 ×1

debian ×1

g++ ×1

lambda ×1

pthreads ×1

shared-ptr ×1

std ×1

templates ×1