简体   繁体   English

启用多线程Eclipse C ++

[英]Enable multithreading Eclipse C++

I have been trying to get a program working in Eclipse C++. 我一直在尝试使程序在Eclipse C ++中运行。 One of the functions uses multithreading from std. 函数之一使用std中的多线程。 Here is the function in the code: 这是代码中的函数:

void PrimeCheck::checkFull(long long int number)
{
    std::thread t1(&PrimeCheck::checkFirstHalf, this, number);
    std::thread t2(&PrimeCheck::checkSecondHalf, this, number);

    t1.join();
    t2.join();
}

When searching for a solution, I came across many solutions, all of which stating to either add a -pthread flag or a -std=c++11 in addition to changing the dialect to C++11. 在寻找解决方案时,我遇到了许多解决方案,所有这些解决方案都说明除了将方言更改为C ++ 11之外,还添加-pthread标志或-std = c ++ 11。 All of which I have done. 我都做了。 This is what the compile command looks like in eclipse so you can see exactly which modifications I have already added: 这是eclipse中的compile命令,因此您可以确切地看到我已经添加了哪些修改:

Building file: ../src/Prime Checker.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -D__GXX_EXPERIMENTAL_CXX0X__ -O2 -g -Wall -c -fmessage-length=0 -std=c++11  -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive -MMD -MP -MF"src/Prime Checker.d" -MT"src/Prime\ Checker.d" -o "src/Prime Checker.o" "../src/Prime Checker.cpp"
Finished building: ../src/Prime Checker.cpp

And this is the linker command as it appears in eclipse: 这是出现在eclipse中的链接器命令:

Invoking: GCC C++ Linker
g++ -Wl,--no-as-needed -pthread -shared -o [A bunch of .o files]

The code compiles correctly, and eclipse content assist recognizes thread as a member of std. 该代码正确编译,并且日食内容辅助将线程识别为std的成员。 Yet, when I run the program I still this error: 但是,当我运行程序时,仍然出现此错误:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted

To test this, I wrote a simple program outside of Eclipse which looked like this: 为了测试这一点,我在Eclipse之外编写了一个简单的程序,如下所示:

#include <thread>
#include <iostream>

using namespace std;

void func1(int x){
    for(int i=0; i<x; i++){
        cout << " " << 1 + i;
    }
}

void func2(){
    for(int j=0; j<5; j++){
        cout << "Standard message! ";
    }
}

int main(){
    int input;
    cout << "Give me a number:" << endl;
    cin >> input;

    thread t1(func1, input);
    thread t2(func2);
    t1.join();
    t2.join();


    return 0;
}

And compiled it in the terminal with this: 并在终端中对此进行编译:

g++ ThreadTest.cpp -o Program.o -std=c++11 -pthread

And the program ran without error. 并且程序运行没有错误。 I think this means that there's something wrong with Eclipse, but I'm not sure. 我认为这意味着Eclipse有问题,但是我不确定。

As a note, I'm doing this on Ubuntu 14.04 with gcc version 4.8.4. 注意,我正在Ubuntu 14.04上使用gcc版本4.8.4进行此操作。 Also, I know that similar questions have been asked, but as far as I can tell, I've implemented those solutions with little success. 另外,我知道有人问过类似的问题,但据我所知,我实施这些解决方案的成功很少。

Help would be appreciated. 帮助将不胜感激。 Thanks! 谢谢!

Solved. 解决了。 Using Eclipse IDE for C/C++ Developers v4.7.3a in Ubuntu 14.04 . Ubuntu 14.04中使用Eclipse IDE for C / C ++ Developers v4.7.3a

1/2. 1/2。 Problem description 问题描述

Just trying to run this sample code: 只是尝试运行以下示例代码:

mutex.cpp : Mutex.cpp

// mutex example
#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <mutex>          // std::mutex

std::mutex mtx;           // mutex for critical section

void print_block (int n, char c) {
  // critical section (exclusive access to std::cout signaled by locking mtx):
  mtx.lock();
  for (int i=0; i<n; ++i) { std::cout << c; }
  std::cout << '\n';
  mtx.unlock();
}

int main ()
{
  std::thread th1 (print_block,50,'*');
  std::thread th2 (print_block,50,'$');

  th1.join();
  th2.join();

  return 0;
}

From: http://www.cplusplus.com/reference/mutex/mutex/ 来自: http : //www.cplusplus.com/reference/mutex/mutex/

It builds and runs just fine on the command line with the following command, but will not run in Eclipse! 它可以使用以下命令在命令行上正常构建和运行,但不会在Eclipse中运行!

Command-line command that builds and runs in a terminal just fine: 在终端中构建并运行的命令行命令就可以了:

g++ -Wall -std=c++11 -save-temps=obj mutex.cpp -o ./bin/mutex -pthread && ./bin/mutex

Eclipse error when I try to run it in Eclipse: 尝试在Eclipse中运行时出现Eclipse错误:

 terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted 

2/2. 2/2。 The solution to make it build and run in Eclipse is as follows: 使它在Eclipse中构建和运行的解决方案如下:

A. Enable pthread for compiler: A.为编译器启用pthread:

Project --> Properties --> C/C++ Build --> Settings --> GCC C++ Compiler --> Miscellaneious --> type in -std=c++11 to the "Other flags" box, and check the box for "Support for pthread (-pthread)". 项目->属性-> C / C ++构建->设置-> GCC C ++编译器->其他->在-std=c++11键入“其他标志”框,然后选中该框用于“支持pthread(-pthread)”。 See yellow highlighting here: 看到黄色突出显示在这里:

在此处输入图片说明

B. Enable pthread for Linker: B.为链接器启用pthread:

Then, withOUT closing this window, also set this setting for the Linker: in center pane: GCC C++ Linker --> General --> check the box for "Support for pthread (-pthread)", as shown here: 然后,在没有关闭此窗口的情况下,还为链接器设置此设置:在中央窗格中:GCC C ++链接器->常规->选中“支持pthread(-pthread)”框,如下所示:

在此处输入图片说明

Click "Apply and Close". 点击“应用并关闭”。 It will now build and run. 现在它将构建并运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM