2012-08-07 23 views
2

非常簡單的代碼,不明白爲什麼編譯器會引發錯誤對我使用的omp atomic captureC++ omp原子捕獲 - 語法?

// my_class.h

class my_class 
{ 

my_class() 
{ } 

static int class_int; 
static int get_next_int(); 

}; 

// my_class.cpp

int my_class::get_next_int() 
{ 
    int next_int; 
    #pragma omp atomic capture 
    next_int = class_int++; 

    return next_int; 
} 

編譯器錯誤:

my_class.cpp: In static member function 'static int 
my_class::get_next_int()': 

my_class.cpp:2069: error: expected end of line before 'capture' 
my_class.cpp:2070: error: invalid operator for '#pragma omp atomic' before '=' token 

也許它有事情做與static?我無法想象爲什麼...

如果我使用pragma omp critical代替,那麼它工作正常(無編譯器錯誤)。

PS我在所有的.h和.cpp #include <omp.h>。和我一起-fopenmp鏈接,像往常一樣

+1

你使用什麼編譯器? – 2012-08-07 16:37:27

+0

g ++(Debian 4.4.5-8)4.4.5 – cmo 2012-08-07 19:37:51

回答

3

capture子句中的OpenMP 3.1中引入。你需要一個兼容的編譯器:自V4.7

  • 英特爾編譯

    • 海合會自10.1
    • 的Oracle Solaris Studio的自v12.3
    • 很多other compilers除了...
    • MSVC(所有版本,包括2012),其中不支持 OpenMP版本高於2.0。

    GCC 4.4.5不支持OpenMP的3.1。它僅支持OpenMP 3.0。

  • +0

    哇。我應該檢查明顯的我想。很明顯,我認爲'capture'已經足夠基本,編譯器/版本無關緊要。 – cmo 2012-08-08 12:45:22

    0

    如果您使用的是微軟的編譯器不支持clausesomp atomic指令。

    更改您的代碼只是#pragma omp atomic並根據MS文檔編譯器會做正確的事。讓我感到懷疑。 :)

    +0

    不錯的想法,但我在Linux(g ++)上。 – cmo 2012-08-07 19:36:45

    +1

    @CycoMatto:你的gcc是否支持capture子句?我看到它在4.8版gcc更新日誌中提到。我沒有Linux機器在這裏檢查了... :) – JimR 2012-08-08 04:11:12