易截截图软件、单文件、免安装、纯绿色、仅160KB

linux下C语言多线程编程实例


兰大论坛上的一个帖子~~
学东西,往往实例才是最让人感兴趣的,老是学基础理论,不动手,感觉没有成就感,呵呵。
下面先来一个实例。我们通过创建两个线程来实现对一个数的递加。
或许这个实例没有实际运用的价值,但是稍微改动一下,我们就可以用到其他地方去拉。
下面是我们的代码:
/*thread_example.c : c multiple thread programming in linux
*author : falcon
*E-mail : tunzhj03@st.lzu.edu.cn
*/
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10
pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;
void *thread1()
{
printf ("thread1 : I'm thread 1\n");
for (i = 0; i < MAX; i++)
{
printf("thread1 : number = %d\n",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(2);
}
printf("thread1 :主函数在等我完成任务吗?\n");
pthread_exit(NULL);
}
void *thread2()
{
printf("thread2 : I'm thread 2\n");
for (i = 0; i < MAX; i++)
{
printf("thread2 : number = %d\n",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(3);
}
printf("thread2 :主函数在等我完成任务吗?\n");
pthread_exit(NULL);
}
void thread_create(void)
{
int temp;
memset(&thread, 0, sizeof(thread)); //comment1
/*创建线程*/
if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2
printf("线程1创建失败!\n");
else
printf("线程1被创建\n");
if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3
printf("线程2创建失败&q


相关文档:

实战Linux Bluetooth编程(三) HCI层编程

1. HCI层协议概述:
HCI提供一套统一的方法来访问Bluetooth底层。如图所示:
从图上可以看出,Host Controller Interface(HCI)  就是用来沟通Host和Module。Host通常就是PC, Module则是以各种物理连接形式(USB,serial,pc-card等)连接到PC上的bluetooth Dongle。
在Host这一端:application,SDP,L2cap等协议 ......

2009年:Linux上网本热度回升

 




<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->

           Linux
上网本能用吗?对此问题,一般人都要打个问号。但是,事实究竟怎样?
 
    2008
年年末和
2009
年年初,
Linux
上 ......

Linux 5.3下配置nis+nfs+autofs


Linux 5.3下配置nis+nfs+autofs
Server(服务端):先装上ypserv服务端软件包 # yum -y install ypserv*
# service ypserv start 
# chkconfig ypserv on
# authconfig-tui 图形程序下配置nis域名和服务端地址
# service ypserv ......

Linux 文件类型 及文件的扩展名

作者:北南南北
来自:LinuxSir.Org
摘要: Linux文件类型常见的有:普通文件、目录、字符设备文件、块设备文件、符号链接文件等,如果想了解这方面知识的弟兄,就进来了解了解。本文应该说是基础文档。
目录
 
2.1 普通文件;
2.2 目录;
2.3 字符设备或块设备文件;
2.4 套接口文件;
2.5 符号链接文件; ......

Linux文件查找命令find,xargs详述

总结:zhy2111314
来自:LinuxSir.Org
整理:北南南北
摘要: 本文是find 命令的详细说明,可贵的是针对参数举了很多的实例,大量的例证,让初学者更为容易理解;本文是zhyfly兄贴在论坛中;我对本文进行了再次整理,为方便大家阅读;
目录
版权声明
前言:关于find命令
一、find 命令格式
1、find命令的一般形式为 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号