Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
启用多线程EclipseC++; 我一直在尝试在Eclipse C++中运行一个程序。其中一个函数使用std中的多线程。以下是代码中的函数: 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(); }_C++_Eclipse_Multithreading_C++11 - Fatal编程技术网

启用多线程EclipseC++; 我一直在尝试在Eclipse C++中运行一个程序。其中一个函数使用std中的多线程。以下是代码中的函数: 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(); }

启用多线程EclipseC++; 我一直在尝试在Eclipse C++中运行一个程序。其中一个函数使用std中的多线程。以下是代码中的函数: 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(); },c++,eclipse,multithreading,c++11,C++,Eclipse,Multithreading,C++11,在搜索解决方案时,我遇到了许多解决方案,除了将方言更改为c++11之外,所有这些解决方案都声明要添加-pthread标志或-std=c++11。所有这些我都做了。这是eclipse中compile命令的外观,因此您可以确切地看到我已经添加了哪些修改: Building file: ../src/Prime Checker.cpp Invoking: GCC C++ Compiler g++ -std=c++0x -D__GXX_EXPERIMENTAL_CXX0X__ -O2 -g -Wall

在搜索解决方案时,我遇到了许多解决方案,除了将方言更改为c++11之外,所有这些解决方案都声明要添加-pthread标志或-std=c++11。所有这些我都做了。这是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
这是eclipse中出现的链接器命令:

Invoking: GCC C++ Linker
g++ -Wl,--no-as-needed -pthread -shared -o [A bunch of .o files]
代码编译正确,eclipse content assist将线程识别为std的一个成员。但是,当我运行程序时,仍然存在以下错误:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
为了测试这一点,我在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;
}
程序运行时没有出错。我认为这意味着Eclipse有问题,但我不确定

请注意,我是在Ubuntu 14.04和gcc版本4.8.4上做这件事的。此外,我知道有人问过类似的问题,但据我所知,我实施这些解决方案几乎没有成功


我们将不胜感激。谢谢

已解决。在Ubuntu 14.04中为C/C++开发人员使用Eclipse IDE v4.7.3a

1/2. 问题描述 正在尝试运行以下示例代码:

互斥体.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;
}
尝试在Eclipse中运行Eclipse时出现Eclipse错误:

2/2. 使其在Eclipse中构建和运行的解决方案如下: A.为编译器启用pthread:

项目>属性-> C/C++构建-->设置> > GCC C++编译器> > MyCuraNealy> >类型>代码> -STD= C++ 11 到“其他标志”框,并选中“支持pToX(-pToX)”框。请参见此处的黄色突出显示:

B.为链接器启用pthread: 然后,在不关闭此窗口的情况下,也为链接器设置此设置: 在中心窗格中:GCC C++链接器>通用> >检查框“支持pToX(-pToX)”,如这里所示:


单击“应用并关闭”。它现在将生成并运行。

已解决。在Ubuntu 14.04中为C/C++开发人员使用Eclipse IDE v4.7.3a

1/2. 问题描述 正在尝试运行以下示例代码:

互斥体.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;
}
尝试在Eclipse中运行Eclipse时出现Eclipse错误:

2/2. 使其在Eclipse中构建和运行的解决方案如下: A.为编译器启用pthread:

项目>属性-> C/C++构建-->设置> > GCC C++编译器> > MyCuraNealy> >类型>代码> -STD= C++ 11 到“其他标志”框,并选中“支持pToX(-pToX)”框。请参见此处的黄色突出显示:

B.为链接器启用pthread: 然后,在不关闭此窗口的情况下,也为链接器设置此设置: 在中心窗格中:GCC C++链接器>通用> >检查框“支持pToX(-pToX)”,如这里所示:


单击“应用并关闭”。它现在将构建并运行。

第一个怀疑是Eclipse调用的g++版本与您从命令行调用g++时使用的版本不同。您将如何验证gcc版本是否相同?在终端中,您可以执行
g++--version
,但在Eclipse中呢?检查您的工具链设置。工具链编辑器只提到它正在使用“Linux GCC”,但这意味着什么?;)使用的可执行文件是什么?第一个怀疑是Eclipse调用的g++版本与从命令行调用g++时使用的版本不同。如何验证gcc版本是否相同?在终端中,您可以执行
g++--version
,但在Eclipse中呢?检查您的工具链设置。工具链编辑器只提到它正在使用“Linux GCC”,但这意味着什么?;)使用的可执行文件是什么?
g++ -Wall -std=c++11 -save-temps=obj mutex.cpp -o ./bin/mutex -pthread && ./bin/mutex
terminate called after throwing an instance of 'std::system_error'  
  what():  Enable multithreading to use std::thread: Operation not permitted