📄 async.examples.html
字号:
<html><!-- #BeginTemplate "/Templates/tmpl.dwt" --><head><!-- #BeginEditable "doctitle" --> <title>PTypes: multithreading: examples</title><!-- #EndEditable --> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link rel="stylesheet" href="styles.css"></head><body bgcolor="#FFFFFF" leftmargin="40" marginwidth="40"><p><a href="../index.html"><img src="title-1.7.gif" width="213" height="34" alt="C++ Portable Types Library (PTypes) Version 1.7" border="0"></a> <hr noshade><!-- #BeginEditable "body" --> <p class="hpath"><a href="index.html">Top</a>: <a href="async.html">Multithreading</a>: Examples </p><p><b>Example 1</b>. This simple example shows the use of mutex objects to synchronize console output. The function <span class="lang">showdiag()</span> can be safely called from any thread, the output from different threads will never be mixed.</p><blockquote> <pre>mutex diagsync;void showdiag(char severity, const char* module, const char* msg){ diagsync.enter(); try { cerr << severity << ' ' << module << ' ' << msg << '\n'; cerr.flush(); } catch(...) { diagsync.leave(); throw; } diagsync.leave();}</pre></blockquote><p><b>Example 2</b>. In this bigger example we use <span class="lang">thread</span>, <span class="lang">semaphore</span>, <span class="lang">message</span> and <span class="lang">msgqueue</span> objects. Here you will find a better idea on how to synchronize diagnostics output: we create a separate <span class="lang">msgqueue</span> object to send messages to console asynchronously, thus freeing all threads from waiting for a <span class="lang">mutex</span> object. Besides dealing with diagnostics output a single message queue can handle many tasks in your application, such like serving as an intermediary between threads, receiving commands to start new threads, etc. </p><blockquote> <pre>#include <iostream.h>#include <ptypes.h>#include <pasync.h>USING_PTYPESconst int MSG_DIAG = MSG_USER + 1;<span class="comment">//// class diagmessage//</span>class diagmessage: public message{protected: string module; string diagstr; friend class diagthread;public: diagmessage(string imodule, string idiagstr) : message(MSG_DIAG), module(imodule), diagstr(idiagstr) {}};<span class="comment">//// class diagthread//</span>class diagthread: public thread, protected msgqueue{protected: virtual void execute(); <span class="comment">// override thread::execute()</span> virtual void cleanup(); <span class="comment">// override thread::cleanup()</span> virtual void msghandler(message& msg); <span class="comment">// override msgqueue::msghandler()</span>public: diagthread(): thread(false), msgqueue() { } inline void postdiag(string module, string diagstr); void postquit();};inline void diagthread::postdiag(string module, string diagstr){ msgqueue::post(new diagmessage(module, diagstr));}void diagthread::postquit(){ msgqueue::post(MSG_QUIT); }void diagthread::execute(){ <span class="comment">// starts message queue processing; calls // msghandler for each message</span> msgqueue::run();}void diagthread::cleanup(){}void diagthread::msghandler(message& msg){ switch (msg.id) { case MSG_DIAG: cout << char(msg.param) << " [" << ((diagmessage&)msg).module << "] " << ((diagmessage&)msg).diagstr << '\n'; cout.flush(); break; default: defhandler(msg); }}<span class="comment">//// class testthread//</span>class testthread: public thread{protected: diagthread& diag; string myname; virtual void execute(); virtual void cleanup();public: semaphore sem; testthread(diagthread& idiag) : thread(false), diag(idiag), myname("testthread"), sem(0) {}};void testthread::execute(){ diag.postdiag(myname, "starts and enters sleep for 1 second"); psleep(1000); diag.postdiag(myname, "releases the semaphore"); sem.post(); diag.postdiag(myname, "enters sleep for 1 more second"); psleep(1000);}void testthread::cleanup(){ diag.postdiag(myname, "terminates");}<span class="comment">//// main//</span>int main(){ diagthread diag; testthread thr(diag); string myname = "main"; diag.start(); thr.start(); diag.postdiag(myname, "waits for the semaphore"); thr.sem.wait(); diag.postdiag(myname, "now waits for testthread to terminate"); <span class="comment">// waitfor() is necessary for static or local thread objects</span> thr.waitfor(); <span class="comment">// can't post messages anymore, diag thread goes down...</span> diag.postquit(); diag.waitfor(); return 0;}</pre></blockquote><p class="seealso">See also: <a href="async.thread.html">thread</a>, <a href="async.semaphore.html">semaphore</a>, <a href="async.mutex.html">mutex</a>, <a href="async.rwlock.html">rwlock</a>, <a href="async.trigger.html">trigger</a>, <a href="async.msgqueue.html">msgqueue</a>, <a href="async.message.html">message</a></p><!-- #EndEditable --><hr size="1"><a href="../index.html" class="ns">PTypes home</a></body><!-- #EndTemplate --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -