condition-variable

    0熱度

    1回答

    我在理解winapi條件變量如何工作時遇到問題。 在更具體的一面,我想要的是一些等待一些條件的線程。然後,我想使用WakeAllConditionVariable()調用來喚醒所有線程,以便它們可以工作。除了我只想讓線程開始的事實之外,他們沒有任何其他先決條件可以開始工作(就像在n生產者/ n用戶場景中那樣)。 這裏是到目前爲止的代碼: #define MAX_THREADS 4 CONDIT

    9熱度

    1回答

    Linux的編程接口書中有一段代碼(生產者/消費者)來顯示條件變量是如何工作的: static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int avail = 0; while (TRUE) { s

    1熱度

    1回答

    多線程代碼段異步訪問資源(例如:文件系統)。 爲了實現這一點,我將使用條件變量。假設FileSystem是一個接口,如: class FileSystem { // sends a read request to the fileSystem read(String fileName) { // ... // upon completion, execu

    3熱度

    2回答

    我注意到當我對條件變量執行等待操作時,它立即返回。其結果是,執行下面的僞代碼時,被在此循環中使用一個CPU的100%: int main(void) { boost::condition_variable cond; boost::mutex mut; bool data_ready = false; boost::unique_lock<boost::mutex> lock(mut)

    5熱度

    2回答

    假設某個條件變量「cond」與一個互斥變量「mutex」關聯。如果一個線程在調用pthread_cond_wait(&cond,&mutex)之後正在睡眠cond,並且鎖定的另一個線程完成鎖定,那麼在調用pthread_mutex_unlock(&mutex)之前還是之後,該線程調用pthread_cond_signal(&cond)是否有關係?如果它調用pthread_cond_signal(&

    2熱度

    1回答

    下面的代碼重新錯誤: #include <iostream> #include "boost/thread.hpp" #include "boost/date_time/posix_time/ptime.hpp" int main() { boost::condition_variable_any cv; boost::timed_mutex m; try

    6熱度

    2回答

    我們可以使用新的條件變量原語或Windows事件來同步WinNT v6.x或更高版本中的線程。考慮以下兩種方法,我們希望工作人員在main中設置「go」時同時運行,否則應全部阻止。 /*language C code*/ /*Windows Condition Variable*/ int go=0; CONDITION_VARIABLE cv; SRWLOCK lock; void w

    9熱度

    4回答

    如果在持續時間爲boost::condition_variable上使用timed_wait,則即使用戶(或ntp)更改系統時間,等待條件是否會超時? 例如, boost::posix_time::time_duration wait_duration(0, 0, 1, 0); // 1 sec // ** System time jumps back 15 minutes here. **

    5熱度

    4回答

    後來我想着如何以各自的方式實現各種同步原語。例如,在pthreads中,你可以獲得互斥鎖和條件變量,並從這些變量中構建信號量。 在Windows API(或至少是舊版本的Windows API)中有互斥鎖和信號量,但沒有條件變量。我認爲應該可以用互斥和信號建立條件變量,但是對於我來說,我只是想不出一個辦法。 有沒有人知道這樣做的好構造?

    1熱度

    2回答

    我想使用Boost庫同步兩個線程(在相同的C++映射上工作)。我必須告訴我,我不是C++的專家,我覺得boost文檔很難理解。 我想實現什麼,是這樣的: #Thread 1 get access to the map put in something release access #Thread 2 wait until map is empty when it's not empt