-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
226 lines (218 loc) · 24.2 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>LDA</title>
<url>/2021/05/04/LDA/</url>
<content><![CDATA[<h1 id="线性判别分析-Linear-Discriminant-Analysis"><a href="#线性判别分析-Linear-Discriminant-Analysis" class="headerlink" title="线性判别分析(Linear Discriminant Analysis)"></a>线性判别分析(Linear Discriminant Analysis)</h1><h2 id="算法目的"><a href="#算法目的" class="headerlink" title="算法目的"></a>算法目的</h2><p>找到易于将数据有效分类的最有效投影方式。</p>
<p><img src="D:\Python\Notes\source\_posts\LDA\图片1.png" alt=""></p>
<h2 id="算法描述"><a href="#算法描述" class="headerlink" title="算法描述"></a>算法描述</h2><p><strong>核心:减小类内差异,增大类间差异。</strong></p>
<p>以二分类为例子。</p>
<p>给定条件如下:</p>
<script type="math/tex; mode=display">
D=\{x_i\}_{i=1}^{n},x_i=(x_i^{(1)};x_i^{(2)};...x_i^{(d)})\\
D_1=\{x_i|x_i \in w_1\}\\
D_2=\{x_i|x_i \in w_2\}</script><p><strong>样本中心:</strong></p>
<script type="math/tex; mode=display">
m_i=\frac{1}{|D_i|}\Sigma_{x\in D_i}x</script><p><strong>投影之后:</strong></p>
<script type="math/tex; mode=display">
y=w^Tx\\</script><script type="math/tex; mode=display">
\hat m_i=\frac{1}{|D_i|}\Sigma_{x\in D_i}y=\frac{1}{|D_i|}\Sigma_{x\in D_i}\\w^Tx=w^Tm_i</script><p><strong>类间散度矩阵:</strong></p>
<script type="math/tex; mode=display">
S_B=(m_1-m_2)(m_1-m_2)^T</script><p><strong>类间差异描述</strong>:</p>
<script type="math/tex; mode=display">
\begin{aligned}
|\hat m_1-\hat m_2|^2&=|w^T(m_1-m_2)|^2\\
&=w^T(m_1-m_2)(m_1-m_2)^Tw\\
&=w^TS_Bw
\end{aligned}</script><script type="math/tex; mode=display">
\begin{aligned}
|\hat m_1-\hat m_2|^2&=|w^T(m_1-m_2)|^2\\
&=w^T(m_1-m_2)(m_1-m_2)^Tw\\
&=w^TS_Bw
\end{aligned}</script><p><strong>类内散度矩阵:</strong></p>
<script type="math/tex; mode=display">
S_i=\Sigma_{x \in D_i}(x-m_i)(x-m_i)^T</script><p><strong>类内差异描述</strong>:</p>
<script type="math/tex; mode=display">
\begin{aligned}
\hat s_i&=\Sigma_{x\in D_i}(y-\hat m_i)^2\\
&=\Sigma_{x\in D_i}(w^Tx-w^Tm_i)^2\\
&=\Sigma_{x\in D_i}w^T(x-w^Tm_i)(x-w^Tm_i)^Tw\\
&=w^TS_iw\\
\end{aligned}</script><p>再令:</p>
<script type="math/tex; mode=display">
S_w=S_1+S_2</script><p>需要同时满足核心目标,则将<strong>Fisher criterion function</strong>定义为:</p>
<script type="math/tex; mode=display">
J(w)=\frac{|\hat m_1-\hat m_2|^2}{\hat s_1^2+\hat s_2^2}</script><p>基于上述式子,可将问题变形为:</p>
<script type="math/tex; mode=display">
J(w)=\frac{w^TS_Bw}{w^TS_ww}</script><p>注意到该式子与$w$的长度无关,只与其方向有关,不妨令$w^TS_ww=1$:</p>
<p>由拉格朗日乘子法知,上式等价于:</p>
<p>$S_bw=\lambda S_ww$</p>
<p>注意到$S_Bw$的方向恒为$m_1-m_2$</p>
<p>再令:</p>
<p>$S_Bw=\lambda (m_1-m_2)$</p>
<p>即得:</p>
<p>$w=S_w^{-1}(m_1-m_2)$</p>
<p><strong>扩展到c类的线性判别函数:</strong></p>
<p>此时问题变为:</p>
<script type="math/tex; mode=display">
J(W)=\frac{W^TS_BW}{W^TS_wW},W \in R^{d*n}</script><p>其中$d \in \{i|1\le i \le c-1\}$,下面来解释$d$值域的由来:</p>
<p>与二类一样,本质上我们需要解的问题为:</p>
<script type="math/tex; mode=display">
S_bW=\lambda S_wW</script><script type="math/tex; mode=display">
S_w^{-1}S_bW=\lambda W</script><p>$S_B=\Sigma_{i=1}^{c}|D_i|(m_i-m)(m_i-m)^T$</p>
<p>$r((m_i-m)(m_i-m)^T)\le 1$,$c$个秩小于$1$的矩阵加和,同时其中只有$c-1$个相互独立的矩阵。</p>
<p>因此$r(S_B)\le c-1$,$r(S_w^{-1}S_b)\le c-1,$只有最多$c-1$个非零特征值,对应的特征向量也最多只有$c-1$个。</p>
<h2 id="对比:-PCA-and-LDA"><a href="#对比:-PCA-and-LDA" class="headerlink" title="对比: PCA and LDA"></a>对比: PCA and LDA</h2><p>经过一系列计算,最终我们想要做的操作基本一致,通过:</p>
<script type="math/tex; mode=display">
y=Wx</script><p>进行投影。</p>
<p>由于<strong>PCA</strong>在原来对所有的样本有过<strong>中心化</strong>的操作,所以运算之后还需要<strong>去中心化</strong>,而LDA则不需要。</p>
]]></content>
<tags>
<tag>Machine Learning</tag>
<tag>Pattern Recognition</tag>
<tag>Dimensionality Reduction</tag>
</tags>
</entry>
<entry>
<title>Modelselection</title>
<url>/2021/04/11/Modelselection/</url>
<content><![CDATA[<h1 id="Measuring-Accuracy-Using-Cross-Validation"><a href="#Measuring-Accuracy-Using-Cross-Validation" class="headerlink" title="Measuring Accuracy Using Cross-Validation"></a>Measuring Accuracy Using Cross-Validation</h1><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.model_selection <span class="keyword">import</span> cross_val_score </span><br><span class="line">cross_val_score(sgd_clf, X_train, y_train_5, cv=<span class="number">3</span>, scoring=<span class="string">"accuracy"</span>)</span><br><span class="line"></span><br><span class="line">array([<span class="number">0.96355</span>, <span class="number">0.93795</span>, <span class="number">0.95615</span>])</span><br></pre></td></tr></table></figure>
<p>this is simply because only about 10% of the images are 5s.(you will get 90% accuracy if you consider every image as not 5)</p>
<h1 id="Confusion-Matrix"><a href="#Confusion-Matrix" class="headerlink" title="Confusion Matrix"></a>Confusion Matrix</h1><p>A much better way to evaluate the performance of a classifier is to look at the confusion matrix.<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.model_selection <span class="keyword">import</span> cross_val_predict </span><br><span class="line">y_train_pred = cross_val_predict(sgd_clf, X_train, y_train_5, cv=<span class="number">3</span>)</span><br></pre></td></tr></table></figure><br>Now you are ready to get the confusion matrix using the confusion_matrix()function.</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.metrics <span class="keyword">import</span> confusion_matrix </span><br><span class="line">confusion_matrix(y_train_5, y_train_pred) </span><br><span class="line"></span><br><span class="line">array([[<span class="number">53057</span>, <span class="number">1522</span>], [ <span class="number">1325</span>, <span class="number">4096</span>]])</span><br></pre></td></tr></table></figure>
<p>i.e.</p>
<script type="math/tex; mode=display">
\begin{bmatrix}
TN&FP\\\
FN&TP
\end{bmatrix}</script><h1 id="Precision-and-Recall"><a href="#Precision-and-Recall" class="headerlink" title="Precision and Recall"></a>Precision and Recall</h1><script type="math/tex; mode=display">
precision=\frac{TP}{TP+FP}</script><script type="math/tex; mode=display">
recall=\frac{TP}{TP+FN}</script><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.metrics <span class="keyword">import</span> precision_score, recall_score</span><br><span class="line">precision_score(y_train_5, y_train_pred) <span class="comment"># == 4096 / (4096 + 1522) </span></span><br><span class="line"><span class="number">0.7290850836596654</span> </span><br><span class="line">recall_score(y_train_5, y_train_pred) <span class="comment"># == 4096 / (4096 + 1325) </span></span><br><span class="line"><span class="number">0.7555801512636044</span></span><br></pre></td></tr></table></figure>
<p>To compute the F1 score, simply call the f1_score() function:<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.metrics <span class="keyword">import</span> f1_score </span><br><span class="line">f1_score(y_train_5, y_train_pred) </span><br><span class="line"></span><br><span class="line"><span class="number">0.7420962043663375</span></span><br></pre></td></tr></table></figure></p>
]]></content>
<tags>
<tag>handson-ml</tag>
<tag>ML</tag>
</tags>
</entry>
<entry>
<title>PCA</title>
<url>/2021/04/29/PCA/</url>
<content><![CDATA[<h1 id="主成分分析(Principal-Component-Analysis-)"><a href="#主成分分析(Principal-Component-Analysis-)" class="headerlink" title="主成分分析(Principal Component Analysis )"></a>主成分分析(Principal Component Analysis )</h1><h2 id="算法目的"><a href="#算法目的" class="headerlink" title="算法目的"></a>算法目的</h2><p>在冗杂的特征维度中,提取有效信息,解决维数灾难问题。</p>
<h2 id="算法原理"><a href="#算法原理" class="headerlink" title="算法原理"></a>算法原理</h2><p>数据集$D=\{x_i|i=0,1,…,n\}$,每个数据点有$d$维数据</p>
<p>我们用一条过样本均值点$\mu=\frac{1}{n}\Sigma_{i=1}^{n}x_i$的直线来表示重构之后的$\hat{x}_k$:</p>
<script type="math/tex; mode=display">
\hat{x}_k=\mu+a_ke</script><p>其中e为单位向量。</p>
<p>为了最小化重构误差,即最小化投影所带来的损失:</p>
<script type="math/tex; mode=display">
\begin{aligned}
J_1(a_1,a_2,...a_n,e)&=||\hat{x}_k-x_k||_2^2\\
&=||\mu+a_ke-x_k||_2^2\\
&=\Sigma_{i=1}^{n}a_k^2-2\Sigma_{i=1}^{n}a_ke^t(x_k-\mu)+\Sigma_{i=1}^{n}||x_k-\mu||_2^2
\end{aligned}</script><script type="math/tex; mode=display">
\frac{\partial J_1}{\partial e}=2a_k-e^t(x_k-\mu)</script><p>得到:</p>
<script type="math/tex; mode=display">
a_k=e^t(x_k-\mu)</script><p>再将这个式子带回loss函数:</p>
<script type="math/tex; mode=display">
J_1(e)=-\Sigma_{i=1}^{n}e^t(x_k-\mu)(x_k-\mu)^Te+\Sigma_{i=1}^{n}||x_k-\mu||_2^2</script><p>令</p>
<script type="math/tex; mode=display">
S=\Sigma_{i=1}^{n}(x_k-\mu)(x_k-\mu)^T</script><p>矩阵S被称作“散布矩阵”。</p>
<script type="math/tex; mode=display">
J_1(e)=-e^tSe+\Sigma_{i=1}^{n}||x_k-\mu||_2^2</script><p>继续用拉格朗日乘子法来最大化$e^tSe$:</p>
<script type="math/tex; mode=display">
u=e^tSe-\lambda(e^te-1)</script><script type="math/tex; mode=display">
\frac{\partial u}{\partial e}=2Se-2\lambda e=0</script><p>很容易看出这里的$\lambda$就是散布矩阵的特征值,且按照优化函数来看,这里的特征值应当取最大的,e就是其对应的特征向量。</p>
<p>我们再把一维的$a_k$拓展到$d^{‘}$维,将$x_k$表示维$y_k=(a_{k1},a_{k2},…,a_{kd^{‘}})^T$</p>
<p>同理前面降维至一维的思路,只不过这一次需要将特征值排序,选前$d^{‘}$个最大的特征值。</p>
<h2 id="如何选择-d-‘"><a href="#如何选择-d-‘" class="headerlink" title="如何选择$d^{‘}$"></a>如何选择$d^{‘}$</h2><p>如果所需保留原来$m\%$信息,则需要找到最小满足条件的$d^{‘}$,使得:</p>
<script type="math/tex; mode=display">
\frac{\Sigma_{i=1}^{d^{'}}\lambda_i}{\Sigma\lambda_i}\geq m\%</script><p>即可。</p>
]]></content>
<tags>
<tag>Machine Learning</tag>
<tag>Pattern Recognition</tag>
<tag>Dimensionality Reduction</tag>
</tags>
</entry>
<entry>
<title></title>
<url>/2021/03/24/Pandas%E4%BD%BF%E7%94%A8/</url>
<content><![CDATA[<hr>
<h2 id="Pandas使用"><a href="#Pandas使用" class="headerlink" title="Pandas使用"></a>Pandas使用</h2><p><strong>一般约定:</strong></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br></pre></td></tr></table></figure>
<h1 id="对pandas库的理解"><a href="#对pandas库的理解" class="headerlink" title="对pandas库的理解"></a>对pandas库的理解</h1><ul>
<li>两个基本数据类型:<strong>Series</strong>,<strong>DataFrame</strong></li>
<li><strong>Series</strong>提供一维数据类型</li>
<li><strong>DataFrame</strong>提供高维数据类型</li>
</ul>
<span id="more"></span>
<h1 id="Series类型"><a href="#Series类型" class="headerlink" title="Series类型"></a>Series类型</h1><p>由一组数据及其索引构成。<br><strong>成员:</strong></p>
<ul>
<li><strong>index</strong>,索引</li>
<li><strong>values</strong>,数据,ndarray类型</li>
</ul>
<p>既有自定义索引,又有如同数组的索引。<br><strong>创建方法:</strong><br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">列表创建</span><br><span class="line">pd.series(<span class="built_in">list</span>,index=<span class="built_in">list</span>)</span><br><span class="line">数组创建</span><br><span class="line">pd.series(ndarray,index=ndarray)</span><br><span class="line">标量创建</span><br><span class="line">pd.series(data,index=<span class="built_in">list</span>)</span><br><span class="line">Python字典创建</span><br><span class="line">pd.series(<span class="built_in">dict</span>)</span><br><span class="line">也可以通过index将先后顺序重新排序:</span><br><span class="line">pd.series(<span class="built_in">dict</span>,index=<span class="built_in">list</span>)</span><br></pre></td></tr></table></figure></p>
<h2 id="Series基本操作"><a href="#Series基本操作" class="headerlink" title="Series基本操作"></a>Series基本操作</h2><p><strong>基本操作与数组和字典类似。</strong><br> <strong>name属性:</strong><br> <strong>series</strong>存在<strong>name</strong>成员,可以对这个序列命名。<br><strong>index</strong>,<strong>values</strong>也都存在<strong>name</strong>成员,打印的时候可以显示在表头</p>
<h3 id="对齐操作"><a href="#对齐操作" class="headerlink" title="对齐操作"></a>对齐操作</h3><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">当出现series+series之类运算,会采用索引对齐相加,生成新的series。</span><br></pre></td></tr></table></figure>
<h1 id="DataFrame"><a href="#DataFrame" class="headerlink" title="DataFrame"></a>DataFrame</h1><p>共用相同索引的多条列组成,类似表格。<br><strong>成员:</strong></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.columns</span><br></pre></td></tr></table></figure>
<h2 id="DataFrame基本操作"><a href="#DataFrame基本操作" class="headerlink" title="DataFrame基本操作"></a>DataFrame基本操作</h2><h3 id="汇总函数"><a href="#汇总函数" class="headerlink" title="汇总函数"></a>汇总函数</h3><p><strong>截断</strong><br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">返回表或者序列的前n行,默认参数为<span class="number">5</span></span><br><span class="line">df.head(n)</span><br><span class="line">返回表或者序列的后n行,默认参数为<span class="number">5</span></span><br><span class="line">df.tail(n)</span><br></pre></td></tr></table></figure><br><strong>基本信息</strong></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">df.info()</span><br><span class="line">df.describe()</span><br></pre></td></tr></table></figure>
]]></content>
</entry>
<entry>
<title>模型评估与选择</title>
<url>/2021/03/29/%E6%A8%A1%E5%9E%8B%E9%80%89%E6%8B%A9%E4%B8%8E%E9%AA%8C%E8%AF%81/</url>
<content><![CDATA[<h1 id="经验误差与过拟合"><a href="#经验误差与过拟合" class="headerlink" title="经验误差与过拟合"></a>经验误差与过拟合</h1><p>训练集上的误差称为“训练误差”(training error),经验误差(empirical error).</p>
<p>新样本上的误差称为“泛化误差”(generalization error)</p>
<h2 id="欠拟合"><a href="#欠拟合" class="headerlink" title="欠拟合"></a>欠拟合</h2><p>对训练样本的一般特质尚未学习完全</p>
<p><strong>造成原因:</strong></p>
<ul>
<li><p>学习能力低下</p>
</li>
<li></li>
</ul>
<h2 id="过拟合"><a href="#过拟合" class="headerlink" title="过拟合"></a>过拟合</h2><p>误把训练样本的一些自身特性当作潜在规律。</p>
<p><strong>造成原因:</strong></p>
<ul>
<li>学习能力过于强大</li>
<li></li>
</ul>
<h1 id="模型评估方法"><a href="#模型评估方法" class="headerlink" title="模型评估方法"></a>模型评估方法</h1><h2 id="留出法"><a href="#留出法" class="headerlink" title="留出法"></a>留出法</h2><h2 id="交叉验证法"><a href="#交叉验证法" class="headerlink" title="交叉验证法"></a>交叉验证法</h2><h2 id="自助法"><a href="#自助法" class="headerlink" title="自助法"></a>自助法</h2>]]></content>
<tags>
<tag>西瓜书</tag>
<tag>机器学习</tag>
</tags>
</entry>
<entry>
<title>Training Model</title>
<url>/2021/04/18/Training%20Model/</url>
<content><![CDATA[]]></content>
<tags>
<tag>handson-ml</tag>
</tags>
</entry>
<entry>
<title>线程相关函数</title>
<url>/2021/03/27/%E7%BA%BF%E7%A8%8B%E7%9B%B8%E5%85%B3%E5%87%BD%E6%95%B0/</url>
<content><![CDATA[<h1 id="pthread-create"><a href="#pthread-create" class="headerlink" title="pthread_create"></a>pthread_create</h1><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><pthread.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_create</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params"> <span class="keyword">pthread_t</span> *<span class="keyword">restrict</span> tidp, <span class="comment">//指向线程标识符的指针。</span></span></span></span><br><span class="line"><span class="function"><span class="params"> <span class="keyword">const</span> <span class="keyword">pthread_attr_t</span> *<span class="keyword">restrict</span> attr, <span class="comment">//线程属性,默认为NULL</span></span></span></span><br><span class="line"><span class="function"><span class="params"> <span class="keyword">void</span> *(*start_rtn)(<span class="keyword">void</span> *), <span class="comment">//线程运行函数的起始地址</span></span></span></span><br><span class="line"><span class="function"><span class="params"> <span class="keyword">void</span> *<span class="keyword">restrict</span> arg <span class="comment">//运行函数的参数。</span></span></span></span><br><span class="line"><span class="function"><span class="params"> )</span></span>;</span><br></pre></td></tr></table></figure>
<h1 id="pthread-join"><a href="#pthread-join" class="headerlink" title="pthread_join"></a>pthread_join</h1><h2 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h2><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><pthread.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_join</span><span class="params">(<span class="keyword">pthread_t</span> thread, <span class="comment">//调用的线程标识符</span></span></span></span><br><span class="line"><span class="function"><span class="params"> <span class="keyword">void</span> **retval)</span></span>;<span class="comment">//用户定义的指针,用来存储被等待线程的返回值。</span></span><br></pre></td></tr></table></figure>
<h2 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h2><p>当前线程会处于阻塞状态,直到被调用的线程结束后,当前线程重新开始执行。</p>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><p><strong>注意</strong>下面程序的输出顺序是不能确定的:</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><pthread.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span>* <span class="title">print1</span><span class="params">(<span class="keyword">void</span>* data)</span></span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"1 "</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span>* <span class="title">print2</span><span class="params">(<span class="keyword">void</span>* data)</span></span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"2 "</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span>* <span class="title">print3</span><span class="params">(<span class="keyword">void</span>* data)</span></span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"3 "</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span>{</span><br><span class="line"> <span class="keyword">pthread_t</span> t,t1,t2;</span><br><span class="line"></span><br><span class="line"> <span class="built_in">pthread_create</span>(&t,<span class="number">0</span>,print1,<span class="literal">NULL</span>);</span><br><span class="line"> <span class="built_in">pthread_create</span>(&t1,<span class="number">0</span>,print2,<span class="literal">NULL</span>);</span><br><span class="line"> <span class="built_in">pthread_create</span>(&t2,<span class="number">0</span>,print3,<span class="literal">NULL</span>);</span><br><span class="line"></span><br><span class="line"> <span class="built_in">pthread_join</span>(t,<span class="literal">NULL</span>);</span><br><span class="line"> <span class="built_in">pthread_join</span>(t1,<span class="literal">NULL</span>);</span><br><span class="line"> <span class="built_in">pthread_join</span>(t2,<span class="literal">NULL</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>创造的多个线程会抢占CPU资源,使得输出不能确定。</li>
<li>在执行到对应线程的pthread_join( )函数时,这个线程可能还没来得及运行就已经结束了。</li>
</ul>
<p>最好将pthread_create( )与pthread_join( )搭配使用。</p>
<p>改写为(按顺序输出的形式):</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="built_in">pthread_create</span>(&t, <span class="number">0</span>, print1, <span class="literal">NULL</span>);</span><br><span class="line"><span class="built_in">pthread_join</span>(t, <span class="literal">NULL</span>);</span><br><span class="line"><span class="built_in">pthread_create</span>(&t1, <span class="number">0</span>, print2, <span class="literal">NULL</span>);</span><br><span class="line"><span class="built_in">pthread_join</span>(t1, <span class="literal">NULL</span>);</span><br><span class="line"><span class="built_in">pthread_create</span>(&t2, <span class="number">0</span>, print3, <span class="literal">NULL</span>);</span><br><span class="line"><span class="built_in">pthread_join</span>(t2, <span class="literal">NULL</span>);</span><br></pre></td></tr></table></figure>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><ul>
<li>[1] <a href="https://blog.csdn.net/wushuomin/article/details/80051295">多线程之pthread_create()函数</a></li>
</ul>
]]></content>
<tags>
<tag>OS</tag>
</tags>
</entry>
</search>