<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhoneGeek 爱疯极客 &#187; 线程</title>
	<atom:link href="http://www.iphone-geek.cn/tag/%e7%ba%bf%e7%a8%8b/feed" rel="self" type="application/rss+xml" />
	<link>http://www.iphone-geek.cn</link>
	<description>iPhone 新闻，编程，技巧与提示，代码，教程</description>
	<lastBuildDate>Thu, 08 Dec 2011 01:18:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NSOperation和NSURLConnection</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/nsoperation%e5%92%8cnsurlconnection</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/nsoperation%e5%92%8cnsurlconnection#comments</comments>
		<pubDate>Wed, 23 Feb 2011 05:48:27 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[基础]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[线程]]></category>
		<category><![CDATA[提示与技巧]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=1128</guid>
		<description><![CDATA[最近一个客户向我汇报了一个程序的问题。经过分析发现问题是NSURLConnection没有正常工作。记得以前测试时没有问题啊。后来上网查到原来在iOS 4.0以后，NSURLConnection无法在NSOperation中正常运行，换句话说，NSURLConnection只能运行于主线程。解决方法其实倒是蛮简单（比如我的程序在download中）：
123456789101112-&#40;void&#41;download
&#123;
&#160; &#160; // NSURLConnectionn won't work if it's not in the main thread
&#160; &#160; if &#40;!&#91;NSThread isMainThread&#93;&#41;
&#160; &#160; &#123;
&#160; &#160; &#160; &#160; &#91;self performSelectorOnMainThread:@selector&#40;download&#41; withObject:nil waitUntilDone:NO&#93;;
&#160; &#160; &#160; &#160; return;
&#160; &#160; &#125;
&#160; &#160; 
&#160; &#160; NSURLRequest *request = &#91;NSURLRequest requestWithURL:&#91;NSURL URLWithString:_urlString&#93;&#93;;
&#160; &#160; _connection = &#91;&#91;NSURLConnection alloc&#93; initWithRequest:request delegate:self&#93;;&#160; &#160; &#160; 
&#125;
]]></description>
			<content:encoded><![CDATA[<p>最近一个客户向我汇报了一个程序的问题。经过分析发现问题是NSURLConnection没有正常工作。记得以前测试时没有问题啊。后来上网查到原来在iOS 4.0以后，NSURLConnection无法在NSOperation中正常运行，换句话说，NSURLConnection只能运行于主线程。解决方法其实倒是蛮简单（比如我的程序在download中）：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>download<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// NSURLConnectionn won't work if it's not in the main thread</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/"><span style="color: #400080;">NSThread</span></a> isMainThread<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>download<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/"><span style="color: #400080;">NSURLRequest</span></a> <span style="color: #002200;">*</span>request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/"><span style="color: #400080;">NSURLRequest</span></a> requestWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span style="color: #400080;">NSURL</span></a> URLWithString<span style="color: #002200;">:</span>_urlString<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; _connection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/"><span style="color: #400080;">NSURLConnection</span></a> alloc<span style="color: #002200;">&#93;</span> initWithRequest<span style="color: #002200;">:</span>request delegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;&nbsp; &nbsp; &nbsp; <br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/nsoperation%e5%92%8cnsurlconnection/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>跨平台代码分享之一 &#8211; 线程</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab-%e7%ba%bf%e7%a8%8b</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab-%e7%ba%bf%e7%a8%8b#comments</comments>
		<pubDate>Sun, 01 Nov 2009 07:40:52 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[线程]]></category>
		<category><![CDATA[跨平台]]></category>

		<guid isPermaLink="false">http://iphonegeekbeta.zuesitech.com/?p=17</guid>
		<description><![CDATA[有些iPhone程序是直接从其他平台移植过来，或者说你喜欢使用cpp，再或者是你打算让你的程序跨越平台，那么使用跨平台的cpp库无疑是一个很好的选择。这就是本系列的主题。
第一篇为大家分享一段本人自用的线程类。它使用pthread，因此可以跨平台使用（windows下需要下载pthread库）。由于程序很简单，就不做什么说明了。

头文件：
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#ifndef _UTILS_THREAD_H_
#define _UTILS_THREAD_H_

#include &#60;pthread.h&#62;

namespace utils
&#123;

class CThread
&#123;

public:
&#160; &#160; typedef void&#40; ThreadCallback&#40; void * &#41; &#41;;

typedef enum
&#123;
&#160; &#160; THREAD_STOP = 0,
&#160; &#160; THREAD_RUN,
&#160; &#160; THREAD_PAUSE
&#125; ThreadState;

CThread&#40;ThreadCallback* callback, void* pUserData&#41;;

~CThread&#40;&#41;;

ThreadState state&#40;&#41; const &#160; &#160; &#160; &#160; &#160; &#123; return m_state; &#160; &#160; &#160; &#125;

ThreadCallback* callback&#40;&#41; const&#160; &#160; &#123; return m_pCallback; &#160; &#125;

void* userData&#40;&#41; const&#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>有些iPhone程序是直接从其他平台移植过来，或者说你喜欢使用cpp，再或者是你打算让你的程序跨越平台，那么使用跨平台的cpp库无疑是一个很好的选择。这就是本系列的主题。</p>
<p>第一篇为大家分享一段本人自用的线程类。它使用pthread，因此可以跨平台使用（windows下需要下载pthread库）。由于程序很简单，就不做什么说明了。</p>
<p><span id="more-17"></span></p>
<h2><span style="color: #000080;"><strong>头文件</strong>：</span></h2>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#ifndef _UTILS_THREAD_H_</span><br />
<span style="color: #339900;">#define _UTILS_THREAD_H_</span><br />
<br />
<span style="color: #339900;">#include &lt;pthread.h&gt;</span><br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #0000ff;">class</span> CThread<br />
<span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">void</span><span style="color: #008000;">&#40;</span> ThreadCallback<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">enum</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; THREAD_STOP <span style="color: #000080;">=</span> 0,<br />
&nbsp; &nbsp; THREAD_RUN,<br />
&nbsp; &nbsp; THREAD_PAUSE<br />
<span style="color: #008000;">&#125;</span> ThreadState<span style="color: #008080;">;</span><br />
<br />
CThread<span style="color: #008000;">&#40;</span>ThreadCallback<span style="color: #000040;">*</span> callback, <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> pUserData<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
~CThread<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
ThreadState state<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_state<span style="color: #008080;">;</span> &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
ThreadCallback<span style="color: #000040;">*</span> callback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_pCallback<span style="color: #008080;">;</span> &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> userData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_pUserdata<span style="color: #008080;">;</span> &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
pthread_t thread<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_thread<span style="color: #008080;">;</span>&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> run<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">void</span> stop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">void</span> pause<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
<br />
&nbsp; &nbsp; ThreadCallback<span style="color: #000040;">*</span> &nbsp; &nbsp; m_pCallback<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; pthread_t &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_thread<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; ThreadState &nbsp; &nbsp; &nbsp; &nbsp; m_state<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span>&nbsp; &nbsp; &nbsp; &nbsp; m_uiThreadId<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_pUserdata<span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #339900;">#endif // _UTILS_THREAD_H_</span></div></td></tr></tbody></table></div>
<h2><span style="color: #000080;">cpp文件:</span></h2>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &quot;utils/Thread.h&quot;</span><br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> threadRun<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>_ptr <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> run <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CThread<span style="color: #000040;">*</span> theThread <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span> CThread<span style="color: #000040;">*</span> <span style="color: #008000;">&#41;</span>_ptr<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> run <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span> theThread<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>state<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">THREAD_RUN</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CThread<span style="color: #008080;">::</span><span style="color: #007788;">ThreadCallback</span><span style="color: #000040;">*</span> callback <span style="color: #000080;">=</span> theThread<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>callback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> callback <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback<span style="color: #008000;">&#40;</span> theThread<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>userData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">THREAD_STOP</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; run <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">case</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">THREAD_PAUSE</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;msleep<span style="color: #008000;">&#40;</span>50<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
CThread<span style="color: #008080;">::</span><span style="color: #007788;">CThread</span><span style="color: #008000;">&#40;</span>ThreadCallback<span style="color: #000040;">*</span> callback, <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> pUserData<span style="color: #008000;">&#41;</span><br />
<span style="color: #008080;">:</span> m_pCallback<span style="color: #008000;">&#40;</span>callback<span style="color: #008000;">&#41;</span>,<br />
&nbsp; m_pUserdata<span style="color: #008000;">&#40;</span>pUserData<span style="color: #008000;">&#41;</span>,<br />
&nbsp; m_state<span style="color: #008000;">&#40;</span>THREAD_PAUSE<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; m_uiThreadId <span style="color: #000080;">=</span> pthread_create<span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>m_thread,<span style="color: #0000ff;">NULL</span>,threadRun, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">this</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
CThread<span style="color: #008080;">::</span>~CThread<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; pthread_join<span style="color: #008000;">&#40;</span> m_thread, <span style="color: #0000ff;">NULL</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">run</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; m_state <span style="color: #000080;">=</span> THREAD_RUN<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; m_state <span style="color: #000080;">=</span> THREAD_STOP<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">pause</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; m_state <span style="color: #000080;">=</span> THREAD_PAUSE<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<h2><span style="color: #000080;">msleep函数：</span></h2>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &amp;lt;sys/time.h&amp;gt;</span><br />
<br />
<span style="color: #0000ff;">void</span> msleep<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ms <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> microsecs<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">struct</span> timeval tv<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; microsecs <span style="color: #000080;">=</span> ms <span style="color: #000040;">*</span> <span style="color: #0000dd;">1000</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; tv.<span style="color: #007788;">tv_sec</span> &nbsp;<span style="color: #000080;">=</span> microsecs <span style="color: #000040;">/</span> <span style="color: #0000dd;">1000000</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; tv.<span style="color: #007788;">tv_usec</span> <span style="color: #000080;">=</span> microsecs <span style="color: #000040;">%</span> <span style="color: #0000dd;">1000000</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; select<span style="color: #008000;">&#40;</span> 0, <span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">NULL</span>, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>tv <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<h2><span style="color: #000080;">使用方法：</span></h2>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> onRun<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<br />
......<br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
utils<span style="color: #008080;">::</span><span style="color: #007788;">CThread</span><span style="color: #000040;">*</span> m_pThread <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> utils<span style="color: #008080;">::</span><span style="color: #007788;">CThread</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>ThreadCallback<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>onRun,<span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
m_pThread<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>run<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
......<br />
<br />
<span style="color: #0000dd;">delete</span> m_pThread<span style="color: #008080;">;</span></div></td></tr></tbody></table></div>
<p>作者:bagusflyer</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab-%e7%ba%bf%e7%a8%8b/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

