标签: stdthread

mingw std::thread 与 Windows API

std::thread到目前为止,我开始使用 C++11 (mingw 4.8)很好。我遇到了 I/O 重叠的情况,sleepEx用于将线程置于可警告的等待状态。这工作得很好,直到QueueUserAPC必须使用,返回“无效句柄错误”。

经过一番搜索发现,std::thread在Windows下使用了pthread库。

有什么方法可以使用 Windows API 调用,它需要一个线程句柄std::thread吗?或者我是否需要坚持使用 Windows 线程来实现重叠 I/O?

windows mingw c++11 stdthread

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

如何使用std :: thread C++生成多个调用相同函数的线程

基本上我想要做的是编写一个生成多个线程的for循环.线程必须多次调用某个函数.换句话说,我需要每个线程在不同的对象上调用相同的函数.我怎么能用std :: thread c ++库做到这一点?

c++ multithreading c++11 stdthread

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

std::thread (可分离)和异常安全

使用防护、RAII 和 try/catch 块来确保线程在所有情况下最终都会加入是一种常见的做法。但是,对于要分离的线程又如何呢?

void aFunction() {
   std::thread t {someOtherFunction}; // someOtherFunction may throw!!!
   t.detach;
}
Run Code Online (Sandbox Code Playgroud)

我是否应该使用某种异常处理机制来确保t即使在发生异常的情况下也能正确分离,或者这并不重要?

c++ multithreading exception c++11 stdthread

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

用于std :: thread的scoped线程包装器

我正在尝试制作一个范围的线程.

#include <iostream>
#include <thread>

class ScopedThread {
 public:
    template< class Function, class... Args>
    explicit ScopedThread( int id, Function&& f, Args&&... args)
        : m_thread( std::ref(f), std::forward<Args>(args)...)
        , id(std::move(id)) {
    }



    int getId() const { return id; }

    ~ScopedThread() { m_thread.join(); }
 private:
    std::thread m_thread;
    int id;

};

class Worker {
 public:
    Worker(int id): thd(id, &Worker::work, this) { }

    void work() {
       for( int i = 0; i < 10; i++)
        std::cout << "I am working" << std::endl;
    }


 private:
    ScopedThread …
Run Code Online (Sandbox Code Playgroud)

multithreading move-semantics c++11 clang++ stdthread

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

std::thread 不是全局变量,但在到达创建它的函数末尾时不会超出范围?

所以我遇到了一些似乎违背了目的std::thread或至少使它不那么方便的东西。

假设我想生成一个std::thread来执行一项任务一次,并且不想在那之后再次担心它。我thread在函数末尾附近创建了这个函数,因此std::thread在我的例子中,它很快就会超出范围,很可能在它thread仍在运行时。这会产生一些解决方案(或者至少我知道的)的问题。

我可以:

A)将其设置为std::thread全局变量,这样它就不会超出范围。

B) 调用join()std::thread违背了生成线程的目的。

还有其他更好的方法来处理这种情况吗?

c++ multithreading c++11 stdthread

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

在 std::thread 中运行时,C++ 成员变量的生命周期是多少?

#include <iostream>
#include <string>
#include <thread>

using namespace std;

struct safe_thread : public thread
{
    using thread::thread;

    safe_thread& operator=(safe_thread&&) = default;

    ~safe_thread()
    {
        if (joinable())
        {
            join();
        }
    }
};

struct s
{
    safe_thread t;
    std::string text = "for whatever reason, this text will get corrupted";

    s() noexcept
    {
        std::cout << text << '\n'; // it works in constructor as expected
        t = safe_thread{ [this]
                         { long_task(); }};
    }

    void long_task()
    {
        for (int i = 0; i < 500; …
Run Code Online (Sandbox Code Playgroud)

c++ lifetime c++11 stdthread

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

如何处理 std::thread 中的 PostMessageThread 消息?

在我的主线程中的某个地方我正在调用PostThreadMessage(). 但我不知道如何在std::thread我已发送到的地方处理它。

我试图std::thread这样处理它:

while(true) {
    if(GetMessage(&msg, NULL, 0, 0)) {
        // Doing appropriate stuff after receiving the message.
    }
}
Run Code Online (Sandbox Code Playgroud)

我从主线程发送消息,如下所示:

PostThreadMessage(thread.native_handle(), WM_CUSTOM_MESSAGE, 0, 0);
Run Code Online (Sandbox Code Playgroud)

我不知道我是否应该像在线程中那样收到该消息。

我想知道的是,如何检查“工作线程”是否正在接收我发送的消息。

c++ winapi multithreading stdthread

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

C++ Boost中的线程数组

我正在尝试使用线程创建一个数组.我的代码看起来像这样:

boost::thread threads[10];

   for(int i = 0; i < 10; i++){
         client c(io_services[i], "www.boost.org", "/");

         threads[i] ( boost::bind(workerFunc, i) );

  }
Run Code Online (Sandbox Code Playgroud)

我收到编译错误:

error: no match for call to ‘(boost::thread) (boost::_bi::bind_t<void, void (*)(int), boost::_bi::list1<boost::_bi::value<int> > >)’
       threads[i] ( boost::bind(workerFunc, i) );
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚我的代码中需要更改的内容.任何帮助将不胜感激.

c++ multithreading boost stdthread

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

如何知道分离的std :: thread是否已完成执行?

我有一个类似下面的函数,其中线程通过使用std :: lock_guard互斥锁获取锁并通过ofstream写入文件.

当当前文件大小增加最大大小时,我创建一个压缩文件的独立线程并终止.

如果日志文件很大(比如说~500MB),压缩大约需要25秒以上.我分离压缩线程,因为没有其他线程(或主要)想要等待此线程完成.

但我需要知道压缩线程在执行以下行之前没有运行:

_compress_thread(compress_log, _logfile).detach();
Run Code Online (Sandbox Code Playgroud)

示例代码段:

    void log (std::string message)
    {
        // Lock using mutex
        std::lock_guard<std::mutex> lck(mtx);

        _outputFile << message << std::endl;
        _outputFile.flush();
        _sequence_number++;
        _curr_file_size = _outputFile.tellp();

        if (_curr_file_size >= max_size) {
            // Code to close the file stream, rename the file, and reopen
            ...


            // Create an independent thread to compress the file since
            // it takes some time to compress huge files.
            if (the_compress_thread_is_not_already_running) //pseudo code
            {
                _compress_thread(compress_log, _logfile).detach();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在上述if条件下,即 …

c++ multithreading stdthread

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

在std :: thread创建的线程中调用pthread_sigmask是一种好习惯吗?

1)我是std :: thread的新手,我想知道调用pthread_sigmask()阻止在所创建的特定线程中的某些信号是否是一个好习惯std::thread

我不希望新线程接收SIGTERM,SIGHUP等信号,因为主进程已经安装了这些信号的处理程序。

因此,调用pthread_sigmask()阻塞由创建的线程中的某些信号是一种好习惯std::thread吗?

2)另外,我相信的效果pthread_sigmask(SIG_BLOCK, &mask, NULL)仅适用于使用创建的线程

std::thread(&Log::rotate_log, this, _logfile, _max_files, _compress).detach();
Run Code Online (Sandbox Code Playgroud)

rotate_log()作为启动函数调用。

并且的效果pthread_sigmask(SIG_BLOCK, &mask, NULL)将不适用于std::thread(&Log::rotate_log, this, _logfile, _max_files, _compress).detach()被调用的线程。

我的理解正确吗?

void rotate_log (std::string logfile, uint32_t max_files, bool compress)
{
    sigset_t mask;

    sigemptyset (&mask);
    sigaddset (&mask, SIGTERM);
    sigaddset (&mask, SIGHUP);
    pthread_sigmask(SIG_BLOCK, &mask, NULL);

    // Do other stuff.
}

void Log::log (std::string message)
    {
        // Lock using mutex
        std::lock_guard<std::mutex> lck(mtx);

        _outputFile << …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading signals pthreads stdthread

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