显示std :: thread函数传递的错误数据。

时间:2018-05-13 14:50:03

标签: c++ multithreading

我正在使用C ++线程。我想显示从SQL数据库传递到Thread函数的每一行并显示它。我的问题是,如果我为每个Thread数组传递它,显示的数据有点多余。为什么我显示错误的数据?

void callThread(char fname[], int age, int i) {
    std::cout << "\tThread : " << std::this_thread::get_id() << std::endl;
    std::cout << fname << age << std::endl;
}


/*Inside main function*/
int i = 0;
int age;
char fname[20];

    while (SQLFetch(sqlStmtHandle) == SQL_SUCCESS) {
        SQLGetData(sqlStmtHandle, 1, SQL_CHAR, fname, sizeof(fname), NULL);
        SQLGetData(sqlStmtHandle, 2, SQL_C_ULONG, &age, sizeof(age), NULL);

        //display query result
        //std::cout << "\nQuery Result:\n\n";
        //std::cout << fname << age << std::endl;

            threadArray[i] = std::thread(callThread, fname, age, i);
            threadArray[i].detach();

        i++;
        printf("\n Counter: %d\n",i);
    }

数据库内容:
enter image description here

示例输出:
    Katrina 23
    Katrina 22

期望输出
    James 23
    Katrina 22

0 个答案:

没有答案