我对什么是线程的概念目前还并不是很清楚,只是按照书上的实例先学写一个多线程程序,让自己有一个线程编程的意思:
//////////////////////////////////////////////////////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:~$
/////////////////////////////////////////////////////////////////////////////