用于计算斐波纳契数的螺纹程序

问题描述 投票:3回答:1

我正在尝试用C ++编写一个程序来计算Fibonacci系列。我创建了一个执行计算和输出的线程。但是我的for循环中没有任何东西似乎被执行了。任何人都可以查看我的代码并告诉我我可能做错了什么?

#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std; 

//iterative with output
DWORD WINAPI fib3(LPVOID param){
double u = 0; 
double v = 1;
double t; 
int upper = *(int*)param;

for(int i = 2; i <= upper; i++){
    cout << v << " "; 
    t = u + v; 
    u = v; 
    v = t; 
    cout << "testing" << endl; 
}
    cout << v << " "; 
    return 0; 
}

int main(int argc, char *argv[]){

cout << "This will compute the fibonacci series.\n" << endl; 
bool done = true; 
double x; 
DWORD ThreadId; 
HANDLE ThreadHandle; 

while(done){

    cout << "Enter a number: "; 
    cin >> x;

    if(x == -1){
        cout << "\nExiting" << endl; 
        return 0; 
    }

    ThreadHandle = CreateThread(NULL, 0, fib3, &x, 0, &ThreadId); 

    if(ThreadHandle != NULL){
        WaitForSingleObject(ThreadHandle, INFINITE); 

        CloseHandle(ThreadHandle); 
    }

}

return 0; 
}
c++ multithreading fibonacci
1个回答
6
投票

您将double的地址传递给CreateThread,然后尝试将其视为线程func中的int *。将double x;改为int x;

© www.soinside.com 2019 - 2024. All rights reserved.