3

我正在尝试用 C++ 编写一个程序来计算斐波那契数列。我创建了一个执行计算和输出的线程。但是我的 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; 
}
4

1 回答 1

6

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

于 2011-02-28T11:43:07.763 回答