您的位置:首页 > 运维架构 > Linux

第一个LINUX多线程程序

2010-07-15 15:48 411 查看
我对什么是线程的概念目前还并不是很清楚,只是按照书上的实例先学写一个多线程程序,让自己有一个线程编程的意思:

//////////////////////////////////////////////////////source code/////////////////////////////////////////////////

#include <pthread.h>

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>

void *thread_function(void *ar)

{

int i;

for(i=0;i<20;i++)

{

printf("this is a thread!/n");

}

return NULL;

}

int main(void)

{

pthread_t mythread;

if(pthread_create(&mythread,NULL,thread_function,NULL))

{

printf("error creating thread./n");

abort();

}

"mypthread_test.c" 33 lines, 473 characters

//////////////////////////////////////////////////////end///////////////////////////////////////////////////////

////////////////////////////////////////////debug///////////////////////////////////////////

fei@fei-pc:~$ vi mypthread_test.c

fei@fei-pc:~$ gcc -lpthread -o mypthread_test mypthread_test.c

fei@fei-pc:~$ ./mypthread_test

This is main process!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

this is a thread!

fei@fei-pc:~$

/////////////////////////////////////////////////////////////////////////////
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: