我在C++(Windows,Visual Studio 2010)中有兩個類,每個都運行不同的線程,我想以簡單的方式在它們之間發送消息。這個想法是,main調用了class2上的讀取,等待class2獲取數據,然後main類接收它並繼續 - 就像一個套接字,但是在同一個程序上的兩個class /線程之間。這可以做到嗎?Windows C++如何調用從一個線程到另一個線程的阻塞讀取?
例子:
class MyClass(){
...
void run(){...}; //runs a thread here that collects data from a network socket
};
int main(){
MyClass *mc = new MyClass();
mc->run();
...
mc->receiveData(); //returns a value AFTER the class gets a hold of it, and blocks in the meantime...
}
有沒有什麼簡單的方法來做到這一點?有點像創建一個套接字,並從中讀取,直到它從網絡接收到數據包/數據纔會返回,除非我想要一個類在本地系統上執行此操作。謝謝!
http://en.wikipedia.org/wiki/Producer-consumer_problem – Michael
'就像一個套接字,但在同一個程序上的兩個類/線程之間。可以這樣做嗎?' - 你可以用套接字來做......在同一個程序中的套接字沒有錯。 – mah
協程模型聽起來很合身。那麼你只需要1個線程。 http://en.wikipedia.org/wiki/Coroutine – Matt