我有Java线程的经验,但想要学习如何在C++ 11中使用它们。我试图做一个简单的线程池,线程创建一次,可以要求执行任务。C++ 11线程未加入
#include <thread>
#include <iostream>
#define NUM_THREADS 2
class Worker
{
public:
Worker(): m_running(false), m_hasData(false)
{
};
~Worker() {};
void execute()
{
m_running = true;
while(m_running)
{
if(m_hasData)
{
m_system();
}
m_hasData = false;
}
};
void stop()
{
m_running = false;
};
void setSystem(const std::function<void()>& system)
{
m_system = system;
m_hasData = true;
};
bool isIdle() const
{
return !m_hasData;
};
private:
bool m_running;
std::function<void()> m_system;
bool m_hasData;
};
class ThreadPool
{
public:
ThreadPool()
{
for(int i = 0; i < NUM_THREADS; ++i)
{
m_threads[i] = std::thread(&Worker::execute, &m_workers[i]);
}
};
~ThreadPool()
{
for(int i = 0; i < NUM_THREADS; ++i)
{
std::cout << "Stopping " << i << std::endl;
m_workers[i].stop();
m_threads[i].join();
}
};
void execute(const std::function<void()>& system)
{
// Finds the first non-idle worker - not really great but just for testing
for(int i = 0; i < NUM_THREADS; ++i)
{
if(m_workers[i].isIdle())
{
m_workers[i].setSystem(system);
return;
}
}
};
private:
Worker m_workers[NUM_THREADS];
std::thread m_threads[NUM_THREADS];
};
void print(void* in, void* out)
{
char** in_c = (char**)in;
printf("%s\n", *in_c);
}
int main(int argc, const char * argv[]) {
ThreadPool pool;
const char* test_c = "hello_world";
pool.execute([&]() { print(&test_c, nullptr); });
}
的这个输出是:
hello_world
Stopping 0
之后,主线程暂停,因为它在等待第一个线程加入(ThreadPool中的析构函数)。出于某种原因,工作人员的m_running
变量未设置为false,这会使应用程序无限期地运行。
它在我的电脑上正常工作。用visual studio构建2013 –
@DavidHaim:代码包含未定义的行为。代码的行为如预期是完全可能的。 Howerver因为对共享变量m_running的访问不同步,编译器可以假定它不会在工作线程中更改。即使编译器没有对其进行优化,CPU也可以采取相同的措施,并且从不更新m_running的高速缓存条目,并使用其他CPU或内核中更改后的值更新。所以最终的行为取决于你的编译器,它的优化和你的CPU。 – Finn