?? usingthreads.html
字號:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Language" content="zh-cn">
<title>使用SDL:線程</title>
</head>
<body bgcolor="#FFF8DC" text="#000000">
<TABLE>
<TR><!--#include file="../menu.tmpl" -->
<TD>
<p align="center">
[<a href="usingcdrom.html">前一頁</a>]
<a href="toc.html"><font color="#8B0000">目錄</font> </a>
[<a href="usingtimers.html">下一頁</a>]
</p>
<h1><font color="#8B0000">使用SDL</font></h1>
<h2>線程</h2>
<table border="0" cellpadding="4">
<tr>
<td valign="top"><ul>
<li><strong>創建簡單的線程</strong></li>
</ul>
<blockquote>
<p>把一個函數作為SDL_CreateThread()的參數就可以創建一個線程。如果調用成功,該函數就
開始并行運行了,使用自己的棧、寄存器等上下文(context),并能象程序其他部分一樣訪問內存和文件句柄。
</p>
</blockquote>
</td>
<td valign="top" width="200" bgcolor="#D3D3D3"><strong>提示:</strong><br>
SDL_CreateThread()的第二個參數將被傳遞給新線程。你可以傳入一個放在棧上的值,也可以傳入一個供線程使用的數據指針。</td>
</tr>
</table>
<table border="0" cellpadding="50">
<tr>
<td valign="top"><font color="#000080"><strong>例程:</strong></font><pre>
<font color="#0000FF">#include</font> "<font color="#000000">SDL_thread.h</font>"
<font color="#008000">int</font> global_data = 0;
<font color="#008000">int</font> thread_func(<font
color="#008000">void *</font>unused)
{
<font color="#008000">int</font> last_value = 0;
<font color="#0000FF">while</font> ( global_data != -1 ) {
<font color="#0000FF">if</font> ( global_data != last_value ) {
printf("數據改變為<font color="#000000"> %d\n</font>", global_data);
last_value = global_data;
}
SDL_Delay(100);
}
printf("<font color="#000000">線程退出</font><font color="#000000">\n</font>");
<font color="#0000FF">return</font>(0);
}
{
SDL_Thread *thread;
<font color="#008000">int</font> i;
thread = SDL_CreateThread(thread_func, NULL);
<font color="#0000FF">if</font> ( thread == NULL ) {
fprintf(stderr, "<font color="#000000">無法創建線程</font><font color="#000000">: %s\n</font>", SDL_GetError());
return;
}
<font color="#0000FF">for</font> ( i=0; i<5; ++i ) {
printf("<font color="#000000">更改數據為</font><font color="#000000"> %d\n</font>", i);
global_data = i;
SDL_Delay(1000);
}
printf("<font color="#000000">通知線程退出</font><font color="#000000">\n</font>");
global_data = -1;
SDL_WaitThread(thread, NULL);
}
</pre>
</td>
</tr>
</table>
<table border="0" cellpadding="4">
<tr>
<td valign="top"><ul>
<li><strong>對資源訪問進行同步 </strong></li>
</ul>
<blockquote>
<p>通過創建互斥體(mutex),并將資源用lock
(SDL_mutexP())和unlock (SDL_mutexV())保護起來,就可以防止多個線程同時訪問該資源。</p>
</blockquote>
</td>
<td valign="top" width="200" bgcolor="#D3D3D3"><strong>提示:</strong><br>
任何可能會被多個線程訪問的數據都應該用mutex保護起來。</td>
</tr>
</table>
<table border="0" cellpadding="50">
<tr>
<td valign="top"><font color="#000080"><strong>例程:</strong></font><pre>
<font color="#0000FF">#include</font> "<font color="#000000">SDL_thread.h</font>"
<font color="#0000FF">#include</font> "<font color="#000000">SDL_mutex.h</font>"
<font color="#008000">int</font> potty = 0;
<font color="#008000">int</font> gotta_go;
<font color="#008000">int</font> thread_func(<font
color="#008000">void *</font>data)
{
SDL_mutex *lock = (SDL_mutex *)data;
<font color="#008000">int</font> times_went;
times_went = 0;
<font color="#0000FF">while</font> ( gotta_go ) {
SDL_mutexP(lock); <font color="#FF0000">/* 鎖住potty */</font>
++potty;
printf("<font color="#000000">線程</font><font color="#000000">%d 正在使用potty\n</font>", SDL_ThreadID());
<font color="#0000FF">if</font> ( potty > 1 ) {
printf("<font color="#000000">哦</font><font color="#000000">,有人在用potty!\n</font>");
}
--potty;
SDL_mutexV(lock);
++times_went;
}
printf("好了<font color="#000000">\n</font>");
<font color="#0000FF">return</font>(times_went);
}
{
<font color="#0000FF">const</font> <font color="#008000">int</font> progeny = 5;
SDL_Thread *kids[progeny];
SDL_mutex *lock;
<font color="#008000">int</font> i, lots;
<font color="#FF0000">/* 創建同步鎖 */</font>
lock = SDL_CreateMutex();
gotta_go = 1;
<font color="#0000FF">for</font> ( i=0; i<progeny; ++i ) {
kids[i] = SDL_CreateThread(thread_func, lock);
}
SDL_Delay(5*1000);
SDL_mutexP(lock);
printf("<font color="#000000">都完成了嗎?\n</font>");
gotta_go = 0;
SDL_mutexV(lock);
<font color="#0000FF">for</font> ( i=0; i<progeny; ++i ) {
SDL_WaitThread(kids[i], &lots);
printf("<font color="#000000">線程</font><font color="#000000"> %d 用過potty %d 次了\n</font>", i+1, lots);
}
SDL_DestroyMutex(lock);
}
</pre>
</td>
</tr>
</table>
<p align="center">
[<a href="usingcdrom.html">前一頁</a>]
<a href="toc.html"><font color="#8B0000">目錄</font> </a>
[<a href="usingtimers.html">后一頁</a>]
</p>
</TABLE>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -