c++ - Pthreads C++ 编译错误

标签 c++ pthreads

我收到错误“对 `pthread_attr_init' 的 undefined reference ”,即使那应该在 pthread.h 中。这是在应该为 Pthreads 设置的 UNIX 环境中。另外, void* 是指向当前矩阵的好方法吗?这是我的代码,有什么想法吗?

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <time.h>
#include <limits.h>
#include <pthread.h>
#include <semaphore.h>

#define MAX_MATRIX_SIZE     256
#define MAX_THREADS         64

using namespace std;

void *StripProcessor(void *);
void Barrier();
void InitMatrix(int rows, int cols);

int main(int argc, char *argv[])
{
    /* Get command line params */
    if (argc != 3) {
        cout << "Error: need 2 command line params." << endl;
        getchar();
        return 1;
    }
    int numThreads = atoi(argv[1]);
    double epsilon = atof(argv[2]);


    /* Locals hang here */
    pthread_mutex_t barrier;
    pthread_cond_t leave;
    int numWaiting = 0;

    int rows;
    int cols;
    int stripSize;
    int lastStripSize;
    int iterations;

    double matrix1[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE];
    double matrix2[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE];
    void *currentMatrix = matrix1; //????????????????
    double stripMaxDiff[MAX_THREADS];

    clock_t start;
    clock_t finish;


    /* Get rows and cols from standard in */
    string buffer;
    getline(cin, buffer);
    istringstream myStream(buffer);
    myStream >> rows;
    myStream >> cols;
    cout << rows << " " << cols << endl;
    getchar();


    /* Set locals */
    if (rows % numThreads == 0) {
        stripSize = rows / numThreads;
        lastStripSize = 0;
    }
    else {
        stripSize = rows / numThreads + 1;
        if (rows % stripSize == 0) {
            numThreads = rows / stripSize;
            lastStripSize = 0;
        }
        else {
            numThreads = rows / stripSize + 1;
            lastStripSize = rows - (stripSize * (numThreads - 1));
        }
    }


    /* Set up threads */
    vector<pthread_t> threadID;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);


    /* Init mutex, CV, and matrix */
    pthread_mutex_init(&barrier, NULL);
    pthread_cond_init(&leave, NULL);
    //InitMatrix(rows, cols);


    /* Create threads and wait for completion */
    start = clock();

    /*for (unsigned int i = 0; i < threadID.size; i++)
        pthread_create(&threadID[i], &attr, StripProcessor, (void *) i);
    for (unsigned int i = 0; i < threadID.size; i++)
        pthread_join(threadID[i], NULL);*/

    finish = clock();


    /* Print results */

}

最佳答案

这将取决于您的平台,但我会尝试将 -lpthread 添加到您的链接命令,因为这是 Linux 和其他一些平台所需要的。您的程序在这里编译得很好,如 g++ foo.cc -lpthread

关于c++ - Pthreads C++ 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1787909/

相关文章:

c - c中的pthread条件等待

c++ - Ruby 中的多维数组类似于 C++

c++ - 除法超出数据类型限制

c++ - 在 C++ 中刷新组合框?

c++ - 哪些低级 Windows 特定函数用于在 Windows 中实现 std::thread/boost::thread/pthread?

c - 关于pthread_cond_wait的用法

c++ - 读取目录中的多个文件以编辑和写入另一组文件

c++ - 如何重载 = 运算符以在 C++ 中通过引用进行复制?

c++ - 并发访问大量项目

multithreading - 需要知道如何中断所有pthread