第一个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:~$
/////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////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:~$
/////////////////////////////////////////////////////////////////////////////
相关文章推荐
- Linux编程实践----第一个多线程程序
- Linux学习笔记20——第一个多线程程序
- linux基础编程 套接字socket 完整的服务器端多线程socket程序
- Linux 下基于多线程服务器/客服端聊天程序源码
- 使用 C++11 编写 Linux 多线程程序
- 初学linux C写的第一个程序,类似于cp命令
- Linux 多线程程序调用malloc,backtrace引发死锁问题的调试
- Linux下编译《UNIX环境高级编程》的第一个程序时出错的处理方法
- Fireprime第一个Linux程序Hello World”
- Linux下使用GCC编译并运行第一个helloworldC++程序
- linux下c++多线程下用CLOCK()函数统计程序运行时间 2012-12-11 12:46:01 分类: C/C++ clock()函数的功能: 这个函数返回从“开启这个程序进程”到
- linux c/c++多线程程序的编写-转
- Linux中gdb程序调试器的命令使用(包括后台进程和多线程的调试)
- Linux多线程实践(10) --使用 C++11 编写 Linux 多线程程序
- Linux下多线程程序崩溃时如何提取出所有线程的函数调用栈(一)
- 第一个多线程程序——使用pthread
- 【面试常见题目之后台知识】Linux多线程与多进程区别全方位解读+程序实例
- Linux和windows编写多线程程序的区别
- 【面试常见题目之后台知识】Linux多线程与多进程区别全方位解读+程序实例
- Linux下多线程程序调试方法