2011-04-22 89 views
0

我對提升非常非常新穎。據我瞭解,boost :: mutex同時擁有lock()和unlock()兩個成員。但是,我收到以下關於它們後面的函數的錯誤消息。我在同一個文件夾的源代碼中運行了'sudo apt-get install libboost-dev'命令。這也是我授予學生的教授代碼。我確信它應該編譯正確。任何幫助將是偉大的!提升編譯問題

錯誤消息:

matrix.cc: In function ‘ void p_scalarproduct_t(int*, int*, int*, int, int, boost::mutex*) ’:

matrix.cc:75: error: ‘ class boost::mutex ’ has no member named ‘ lock

matrix.cc:77: error: ‘ class boost::mutex ’ has no member named ‘ unlock

matrix.cc: In function ‘ int p_scalarproduct(int*, int*, int, int) ’:

matrix.cc:91: error: ‘ bind ’ is not a member of ‘ boost

代碼:

void p_scalarproduct_t(int* c, int* a, int* b, 
         int s, int e, boost::mutex* lock) 
{ 
    int tmp; 

    tmp = 0; 
    for (int k = s; k < e; k++) 
     tmp += a[k] * b[k]; 

    lock->lock(); 
    *c = *c + tmp; 
    lock->unlock(); 
} 
+0

您使用的是什麼版本的boost? (檢查'boost/version.hpp'找出) – ildjarn 2011-04-22 22:35:33

+0

它說1.34,但我知道這不是最新版本。我怎麼能更新這個? – tpar44 2011-04-22 22:51:33

+2

@ tpar44:有問題; boost.thread在1.35從頭開始被重寫。您可以參考[適用於您的版本的文檔](http://www.boost.org/doc/libs/1_34_1/libs/thread/doc/index.html),或者您可以[從當前版本的升級版本源(http://www.boost.org/doc/libs/release/more/getting_started/unix-variants.html)。 – ildjarn 2011-04-22 22:55:39

回答

1

要鎖定在提升的鎖,你需要將它傳遞給相關scoped_lock,在這種情況下boost::mutex::scoped_lock。所以要鎖上鎖l_,請這樣做:

boost::mutex::scoped_lock l(l_) 
+0

但現在我很困惑。我的教授唯一的東西是放在函數boost :: mutex * lock中,所以我必須放入lock()中? – tpar44 2011-04-23 18:02:16

+0

@ tpar44 boost :: mutex :: scoped_lock local_lock(* lock) – rlc 2011-04-24 14:21:47

+0

@ tpar44這個想法是用作用域鎖取代你的鎖 - > lock()並移除要解鎖的調用。 RAII將負責解鎖。如果您需要限制鎖的範圍,請在其周圍放置大括號{}:當scoped_lock超出範圍時,互斥鎖將被解鎖。 – rlc 2011-04-24 14:26:16