<?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>中文 &#187; 21cnshorer</title>
	<atom:link href="http://software.intel.com/zh-cn/blogs/author/21cnshorer/feed/" rel="self" type="application/rss+xml" />
	<link>http://software.intel.com/zh-cn/blogs</link>
	<description></description>
	<lastBuildDate>Mon, 28 May 2012 13:40:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>C语言中的多线程编程</title>
		<link>http://software.intel.com/zh-cn/blogs/2011/04/28/c-9/</link>
		<comments>http://software.intel.com/zh-cn/blogs/2011/04/28/c-9/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 14:14:22 +0000</pubDate>
		<dc:creator>21cnshorer</dc:creator>
				<category><![CDATA[博客征文专栏]]></category>
		<category><![CDATA[并行计算]]></category>

		<guid isPermaLink="false">http://software.intel.com/zh-cn/blogs/2011/04/28/c-9/</guid>
		<description><![CDATA[很久很久以前，我对C语言的了解并不是很多，我最早听说多线程编程是用Java，其实C语言也有多线程编程，而且更为简单、方便、强大。下面就让我们简单领略一下Unix C语言环境下的多线程编程吧！ 下面先看一个简单的单线程程序： /* 06.3.6 Sghello.c Hello,world -- Single Thread */ #include #define NUM 6 int main() { void print_msg(char*); print_msg("hello,"); print_msg("world!"); } void print_msg(char* m) { int i; for(i=0;i&#60;NUM;i++) { printf("%s",m); fflush(stdout); sleep(1); } } 下图反映了程序的执行流程： 执行结果： $ ./sghello.exe hello,hello,hello,hello,hello,hello,world!world!world!world!world!world! 那么如果想同时执行两个对于print_msg函数的调用，就想使用fork建立两个新的进程一样，那该怎么办？这种思想清楚的体现在下图： 那么怎么才能达到这种效果呢？ 我们可以使用函数pthread_create创建一个新的线程。 函数原型： int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*func)(void*), void *arg); 参数： [...]]]></description>
			<content:encoded><![CDATA[<p>很久很久以前，我对C语言的了解并不是很多，我最早听说多线程编程是用Java，其实C语言也有多线程编程，而且更为简单、方便、强大。下面就让我们简单领略一下Unix C语言环境下的多线程编程吧！<br />
下面先看一个简单的单线程程序：<br />
/* 06.3.6<br />
Sghello.c<br />
Hello,world -- Single Thread<br />
*/<br />
#include<br />
#define NUM 6<br />
int main()<br />
{<br />
void print_msg(char*);<br />
print_msg("hello,");<br />
print_msg("world!");<br />
}<br />
void print_msg(char* m)<br />
{<br />
int i;<br />
for(i=0;i&lt;NUM;i++)<br />
{<br />
printf("%s",m);<br />
fflush(stdout);<br />
sleep(1);<br />
}<br />
}<br />
下图反映了程序的执行流程：<br />
<img src="http://blog.csdn.net/images/blog_csdn_net/mayo/FIG14-01.gif" alt="" /><br />
执行结果：<br />
$ ./sghello.exe<br />
hello,hello,hello,hello,hello,hello,world!world!world!world!world!world!</p>
<p>那么如果想同时执行两个对于print_msg函数的调用，就想使用fork建立两个新的进程一样，那该怎么办？这种思想清楚的体现在下图：<br />
<img src="http://blog.csdn.net/images/blog_csdn_net/mayo/FIG14-02.gif" alt="" /><br />
那么怎么才能达到这种效果呢？<br />
我们可以使用函数pthread_create创建一个新的线程。<br />
函数原型：<br />
int pthread_create(pthread_t *thread,<br />
pthread_attr_t *attr,<br />
void *(*func)(void*),<br />
void *arg);<br />
参数： thread 指向pthread_t类型变量的指针<br />
attr 指向pthread_attr_t类型变量的指针，或者为NULL<br />
func 指向新线程所运行函数的指针<br />
arg 传递给func的参数<br />
返回值 0 成功返回<br />
errcode 错误<br />
我们可以使用函数pthread_join等待某进程结束。<br />
函数原型：int pthread_join(pthread_t thread,void ** retval);<br />
参数： thread 所等待的进程<br />
retval 指向某存储线程返回值的变量<br />
返回值： 0 成功返回<br />
errorcode 错误<br />
以上两个函数都包含在头文件pthread.h中。<br />
下面请看多线程版的Hello,world!<br />
/* 06.3.6<br />
Mhello1.c<br />
Hello,world -- Multile Thread<br />
*/<br />
#include<br />
#include<br />
#define NUM 6<br />
int main()<br />
{<br />
void print_msg(void*);</p>
<p>pthread_t t1,t2;<br />
pthread_create(&amp;t1,NULL,print_msg,(void *)"hello,");<br />
pthread_create(&amp;t2,NULL,print_msg,(void *)"world!\n");</p>
<p>pthread_join(t1,NULL);<br />
pthread_join(t2,NULL);<br />
}<br />
void print_msg(void* m)<br />
{<br />
char *cp=(char*)m;<br />
int i;<br />
for(i=0;i&lt;NUM;i++)<br />
{<br />
printf("%s",m);<br />
fflush(stdout);<br />
sleep(1);<br />
}<br />
}<br />
运行结果：<br />
$ gcc mhello1.c -o mhello1.exe<br />
$ ./mhello1.exe<br />
hello,world!<br />
hello,world!<br />
hello,world!<br />
hello,world!<br />
hello,world!<br />
hello,world!</p>
<p>C语言有一次拓展了我的视野，多线程的问题还有很多，像线程间的分工合作、使用互斥机制保证线程间数据的安全共享、使用条件变量同步线程间的数据传输、传递多个参数给线程等，若读者有兴趣，可自行深入。<br />
推荐《Understanding Unix/Linux Programming》</p>
]]></content:encoded>
			<wfw:commentRss>http://software.intel.com/zh-cn/blogs/2011/04/28/c-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

