标签: stdthread

std::thread,在线程中引发异常会导致 Visual C++ 中的中止错误

我一直在尝试 std:thread。我使用二进制表达式树进行标准算术运算。我正在创建一个线程来执行计算并想要检查是否被零除。当线程以 启动时std::async,异常会从工作线程中抛出,并在主线程中很好地捕获。当我使用 std::thread 启动线程时,抛出异常时,我收到运行时错误abort()。关于它为什么与std::async but notstd::thread` 一起工作的任何见解?

// Declaration in the Expression.h file
public:
    static long double __stdcall ThreadStaticEntryPoint(void * pThis);


long double __stdcall Expression::ThreadStaticEntryPoint(void * pThis)
{
    long double calc;

    Expression* pthrdThis = (Expression*)pThis;
    calc = pthrdThis->Calculate();

    return calc;
}

case 6:
    try {
    // Below works when encountering divide by zero.
    // The thrown exception is caught correctly  
    // Launch thread using the this pointer

        std::future<long double> fu = std::async(std::launch::async,
            ThreadStaticEntryPoint, …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11 stdthread

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

为什么 std::thread 可以返回但不可复制的限制?

我对下面代码的成功编译感到非常困惑。函数“g”中的变量“t”显然是左值。当'g'返回到主函数时,'t'应该被复制。但是线程对象是不可复制的,那为什么会编译成功呢?

#include <thread>

void some_other_function(int) {
    return;
}
std::thread g()
{
    std::thread t(some_other_function,42);
    return t;
}

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

c++ compilation c++11 stdthread

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

std :: this_thread :: yield只是一个提示有问题吗?

同事和我讨论了一个假想的问题,当一个人想要实现自旋锁互斥std::atomic_flag,但也实现那个自旋锁而不是(真)但作为一个

while(true)
{
     cnt=0;
     while (cnt<yieldAfterTries)
     {
        //try to get lock
        cnt++;
     }
     std::this_thread::yield();

     // if got lock do work and then break;
}     
Run Code Online (Sandbox Code Playgroud)

基本上的想法是线程不能"很长一段时间"阻止其他人,即使它有一个实时优先级,因为它会在一段时间后产生...但是当我看到std :: yield的规范时我很惊讶它是一个建议,不是强制性的.

提供实现的提示,以重新安排线程的执行,允许其他线程运行.

http://en.cppreference.com/w/cpp/thread/yield

那会有问题吗?

c++ c++11 stdthread stdatomic

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

标准C++线程ID - 奇怪的行为

我需要按需创建一些线程,因为我不知道我需要多少.std::vector<std::thread> threads; 每次我创建一个新线程时,使用向量"存储" 线程我将它们推回到向量上:threads.push_back(std::thread(worker));

存储(用于测试)线程id我使用以下内容:double test = std::hash<std::thread::id>()(std::this_thread::get_id()); 根据cpp-references hash,thread :: idget_id,这应该可以正常工作.

但只有这条线我得到的ID总是0(零)

如果id在哈希行下面添加以下行,它会工作,我从线程中得到一个哈希的id:std::thread::id tid = std::this_thread::get_id(); 即使我不使用tid

有人可以解释这种行为吗?我正在使用eclipse juno并清理+重建项目几次...我只是不明白:/

这里的代码:(逐行,因为这里的格式规则是愚蠢的 - .-

std::vector<std::thread> threads;
void worker (){
    double test = std::hash<std::thread::id>()(std::this_thread::get_id());
    std::thread::id tid = std::this_thread::get_id();
    printf("%ld\n", test);
}
void joinThreads(std::thread& t)
{
    t.join();
}
int main() {
    for (int i = 0; i < 10; ++i) {
        threads.push_back(std::thread(worker) );
    }
    std::for_each(threads.begin(),threads.end(),joinThreads);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11 stdthread

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

无法理解std :: thread的用法

我正在阅读有关c ++ 11多线程的文档,并且遇到了这个例子std::thread.

码:

void thread_task(int n) {
  ...
}

int main(int argc, const char *argv[])
{
    std::thread threads[5];
    for (int i = 0; i < 5; i++) {
        threads[i] = std::thread(thread_task, i + 1);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我不明白threads[i] = std::thread(thread_task, i + 1);.是std::thread一个静态函数调用,并返回std :: thread对象的引用?听起来不可思议,但似乎是代码所说的.

因为我会这样写:

std::thread *threads[5];
for (int i = 0; i < 5; i++) {
    threads[i] = new std::thread(thread_task, i + 1);
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

c++ c++11 stdthread

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

C++中的std :: thread库是否支持嵌套线程?

我想使用std::thread像这样的库在C++中创建嵌套线程.

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

void innerfunc(int inp)
{
    cout << inp << endl;
}
void outerfunc(int inp)
{
    thread * threads = new thread[inp];
    for (int i = 0; i < inp; i++)
        threads[i] = thread(innerfunc, i);
    for (int i = 0; i < inp; i++)
        threads[i].join();
    delete[] threads;
}
int main()
{
     int inp = 0;
     thread t1 = thread(outerfunc,2);
     thread t2 = thread(outerfunc,3);
     t1.join();
     t2.join();
}
Run Code Online (Sandbox Code Playgroud)

我可以安全地这样做吗?我担心是否join()正常工作.

c++ multithreading c++11 stdthread

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

错误:没有匹配函数调用std :: thread

我试图运行一个dcp在线程中调用的函数,我必须独立运行该函数三次。所以这是我的实现方式:

void dcp(cv::Mat&, int, int, cv::Mat&, double);

int main(int argc, char* argv[])
{
cv::Mat IllumTrans;
//fill IllumTrans

std::vector<cv::Mat> rgbDCP;
rgbDCP.reserve(3);
//Fill it

std::thread thread_1(dcp, rgb[0], rows, cols, IllumTrans, A[0]);
std::thread thread_2(dcp, rgb[1], rows, cols, IllumTrans, A[1]);
std::thread thread_3(dcp, rgb[2], rows, cols, IllumTrans, A[2]);

thread_1.join();
thread_2.join();
thread_3.join();
}
Run Code Online (Sandbox Code Playgroud)

但是我得到了没有匹配函数的错误调用:

In file included from 21022018WorksfineOneimageThread.cpp:6:0:
/usr/include/c++/7/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >’:
/usr/include/c++/7/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading stdthread

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

在运行之前获取std :: thread的线程:id?

我正在尝试在C++ 11之上构建一个线程安全层,std::thread其中每个对象都被分配给一个拥有的线程,并且某些调用在错误的线程上使用时会引发硬错误.拥有线程是唯一可以将对象传输到另一个线程的线程.

我已经完成了所有工作,除了thread::id在实际运行之前我找不到获取线程的方法.我需要先将新线程的ID附加到对象上,然后再将其移除.

如果我使用

std::thread newThread( [theObject]()
                       {
                           // use theObject here.
                       } );
Run Code Online (Sandbox Code Playgroud)

我可以获得线程ID的最早点是在线程对象的定义之后,此时线程已经在运行.

我看到有一个默认的构造函数std::thread,但我看不到有办法给它一个函数来在线程上运行.

有没有办法在线程上执行两步构造,或者在创建时控制线程的ID?

c++ multithreading stdthread

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

线程作为类的成员变量

我想在某个类的成员变量中保留一个线程。以下代码段显示了我想要实现的目标:

#include <iostream>
#include <thread>
#include <vector>


class Test {

    public:
    std::thread& t;    
    Test(std::thread&& rt) : t(rt) {}    
};

int main()
{
    std::vector<Test> tests;

    {
        std::thread t ([]{
            std::cout << 1;
        });
        tests.push_back(Test(std::move(t)));
    }   

    for(Test mytest : tests)
    {
        mytest.t.join();
    }

}
Run Code Online (Sandbox Code Playgroud)

该代码将在join()行处中断。错误是:

terminate called without an active exception
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)

保留原始线程创建的范围后,为什么不能通过mytest.t调用线程?

c++ c++11 stdthread

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

有一个线程拥有它运行C ++的函子

如果要在c ++中的新线程中运行函子,则必须创建函子对象,然后将对它的引用传递给线程构造函数。这可以工作,但是将线程和函子对象作为单独的东西留给您。是否有可能拥有一个函子本身的线程,当在该线程上调用join时,该函子会被清除?可能的API之类的东西可能thread<FunctorType>(args, for, functor)会在线程类中创建functor对象,然后运行它。

c++ multithreading stdthread c++17

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

标签 统计

c++ ×10

stdthread ×10

c++11 ×7

multithreading ×6

c++17 ×1

compilation ×1

stdatomic ×1