在创建线程时抛出异常 C++ std::thread
Exception thrown on thread creation C++ std::thread
我正在尝试使用 std::thread
在 C++ 上生成一些线程,但我无法让它工作。如果它有任何不同,我正在使用 VS 2015。它在创建线程时失败( t[thread_id] = std::thread(test);
)。这是我的代码的相关部分:
void test() {}
void threaded_DFT(std::complex<double>* x, std::complex<double>* X, size_t N) {
std::complex<double>* tmp=(std::complex<double>*)malloc(N * sizeof *tmp);
std::thread* t=NULL;
size_t num_threads;
size_t stages = log2(N);
size_t FFT_8_stages = stages / 3;
size_t remainder_stages = stages % 3;
size_t Ns = 1;
for (size_t i=0, Ns = 1; i < FFT_8_stages; i++,Ns=pow(2,3*i))
{
num_threads = N / 8;
t = (std::thread*)malloc(num_threads * sizeof *t);
if (!t)
exit(EXIT_FAILURE);
for (size_t thread_id = 0; thread_id < num_threads; thread_id++) {
t[thread_id] = std::thread(test);
//t[thread_id] = std::thread(FFT_8, x, X, N, Ns, thread_id);
}
for (size_t thread_id = 0; thread_id < num_threads; thread_id++) {
t[i].join();
}
x = X;
X = tmp;
tmp = x;
}
free(t);
...}
这是调用堆栈:
ucrtbased.dll!00007ffd296a21c5() Unknown
ucrtbased.dll!00007ffd296a2363() Unknown
ucrtbased.dll!00007ffd296c388d() Unknown
ucrtbased.dll!00007ffd296c28f6() Unknown
SignalPlot.exe!std::thread::_Move_thread(std::thread & _Other) Line 111 C++
SignalPlot.exe!std::thread::operator=(std::thread && _Other) Line 68 C++
SignalPlot.exe!threaded_DFT(std::complex<double> * x, std::complex<double> * X, unsigned __int64 N) Line 224 C++
SignalPlot.exe!main(int argc, char * * argv) Line 322 C++
SignalPlot.exe!WinMain(HINSTANCE__ * __formal, HINSTANCE__ * __formal, char * __formal, int __formal) Line 113 C++
这应该很简单,所以我猜有些明显的东西我没有注意到。
如果您对任何标准库 类 使用 malloc 而不是 new,您的代码将无法正常工作。 malloc 不调用构造函数,所有标准库 类 都绝对需要调用它们。为什么你会想到做这样的事情?
我正在尝试使用 std::thread
在 C++ 上生成一些线程,但我无法让它工作。如果它有任何不同,我正在使用 VS 2015。它在创建线程时失败( t[thread_id] = std::thread(test);
)。这是我的代码的相关部分:
void test() {}
void threaded_DFT(std::complex<double>* x, std::complex<double>* X, size_t N) {
std::complex<double>* tmp=(std::complex<double>*)malloc(N * sizeof *tmp);
std::thread* t=NULL;
size_t num_threads;
size_t stages = log2(N);
size_t FFT_8_stages = stages / 3;
size_t remainder_stages = stages % 3;
size_t Ns = 1;
for (size_t i=0, Ns = 1; i < FFT_8_stages; i++,Ns=pow(2,3*i))
{
num_threads = N / 8;
t = (std::thread*)malloc(num_threads * sizeof *t);
if (!t)
exit(EXIT_FAILURE);
for (size_t thread_id = 0; thread_id < num_threads; thread_id++) {
t[thread_id] = std::thread(test);
//t[thread_id] = std::thread(FFT_8, x, X, N, Ns, thread_id);
}
for (size_t thread_id = 0; thread_id < num_threads; thread_id++) {
t[i].join();
}
x = X;
X = tmp;
tmp = x;
}
free(t);
...}
这是调用堆栈:
ucrtbased.dll!00007ffd296a21c5() Unknown
ucrtbased.dll!00007ffd296a2363() Unknown
ucrtbased.dll!00007ffd296c388d() Unknown
ucrtbased.dll!00007ffd296c28f6() Unknown
SignalPlot.exe!std::thread::_Move_thread(std::thread & _Other) Line 111 C++
SignalPlot.exe!std::thread::operator=(std::thread && _Other) Line 68 C++
SignalPlot.exe!threaded_DFT(std::complex<double> * x, std::complex<double> * X, unsigned __int64 N) Line 224 C++
SignalPlot.exe!main(int argc, char * * argv) Line 322 C++
SignalPlot.exe!WinMain(HINSTANCE__ * __formal, HINSTANCE__ * __formal, char * __formal, int __formal) Line 113 C++
这应该很简单,所以我猜有些明显的东西我没有注意到。
如果您对任何标准库 类 使用 malloc 而不是 new,您的代码将无法正常工作。 malloc 不调用构造函数,所有标准库 类 都绝对需要调用它们。为什么你会想到做这样的事情?