- 浏览: 854319 次
- 性别:
- 来自: 济南
文章分类
- 全部博客 (280)
- java相关 (29)
- linux系统 (55)
- ipsec (0)
- ssl (3)
- 信息安全 (13)
- web相关 (35)
- windows (9)
- c,c++ (14)
- log4j (3)
- hibernate (8)
- sqlite (1)
- 程序人生 (2)
- js (2)
- 生活点滴 (3)
- 网络相关 (14)
- 问题积累 (15)
- 数据库相关 (27)
- 软件测试 (2)
- PKI相关 (2)
- 程序设计 (7)
- 犄角旮旯 (0)
- 杂七杂八 (0)
- 硬件相关 (14)
- 防火墙 (2)
- 电子商务 (2)
- 多媒体相关 (1)
- 内存管理 (4)
- 面向对象 (1)
- csp (1)
- 驱动开发 (3)
- 移动开发 (2)
- openssl多线程实例 (1)
最新评论
-
xiaoyao3857:
博主很有探索精神嘛,学习了
Linux主机名Hostname详解 -
hiskyrisa:
言简意赅,好文章。
Flex是什么 -
layznet:
犯了类似错误。使用的是derby数据库。driverClass ...
java.sql.SQLException: No suitable driver -
idision:
你好,文章有一句createSubjectKeyId(keyT ...
bouncycastle 产生证书 -
zheng12tian:
代码有全的不?只贴一部分,,,,
在LOG4J中把日志写入远程数据库
通过创建两个线程来实现对一个数的递加。
或许这个实例没有实际运用的价值,但是稍微改动一下,我们就可以用到其他地方去拉。
- #include <pthread.h>
- #include <stdio.h>
- #include <sys/time.h>
- #include <string.h>
- #include <unistd.h>
- #define MAX 10
- pthread_t thread[2];
- pthread_mutex_t mut;
- int number=0, i;
- void *thread1(void *)
- {
- 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 :the main function is waiting for me?\n");
- pthread_exit(NULL);
- }
- void *thread2(void *)
- {
- 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 :the main function is waiting for me? \n");
- pthread_exit(NULL);
- }
- void thread_create(void)
- {
- int temp;
- memset(&thread, 0, sizeof(thread)); //comment1
- /*Create thread*/
- if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2
- printf("Thread 1 fail to create!\n");
- else
- printf("Thread 1 created!\n");
- if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3
- printf("Thread 2 fail to create!");
- else
- printf("Thread 2 created!\n");
- }
- void thread_wait(void)
- {
- /*Wait thread end*/
- if(thread[0] !=0) { //comment4
- pthread_join(thread[0],NULL);
- printf("Thread 1 completed\n");
- }
- if(thread[1] !=0) { //comment5
- pthread_join(thread[1],NULL);
- printf("Thread 2 completed.\n");
- }
- }
- int main()
- {
- /*init mutex*/
- pthread_mutex_init(&mut,NULL);
- printf("Main function,creating thread...\n");
- thread_create();
- printf("Main function,waiting for thread end....\n");
- thread_wait();
- return 0;
- }
我是主函数哦,我正在创建线程,呵呵
线程1被创建
线程2被创建
我是主函数哦,我正在等待线程完成任务阿,呵呵
thread1 : I'm thread 1
thread1 : number = 0
thread2 : I'm thread 2
thread2 : number = 1
thread1 : number = 2
thread2 : number = 3
thread1 : number = 4
thread2 : number = 5
thread1 : number = 6
thread1 : number = 7
thread2 : number = 8
thread1 : number = 9
thread2 : number = 10
thread1 :主函数在等我完成任务吗?
线程1已经结束
thread2 :主函数在等我完成任务吗?
线程2已经结束
发表评论
-
linux下进程的最大线程数、进程最大数、进程打开的文件数
2011-06-22 17:35 1911linux 系统中单个进程的 ... -
Linux中dos2unix的批量转换命令
2011-01-05 10:31 6925在Linux中将DOS 格式转换为Unix格式的文件可以使用d ... -
linux双网关双线路网络设置
2010-08-03 19:10 1981由于电信和网通之间互联互通 的问题,很多 ... -
syslog-ng学习心得之二
2010-07-06 08:58 2410在[url]http://coolerfeng.blog.51 ... -
syslog-ng学习心得之一
2010-07-06 08:58 2713转载一篇syslog-ng的文章的,本来是想打算自己写一篇 ... -
Linux用户进程是如何释放内存
2009-12-25 17:16 2018Linux进程使用内存的基本流程: 见图1 从 ... -
Linux内存使用详解
2009-12-25 16:56 2347一提到内存管理,我们 ... -
linux下查看内存使用情况
2009-12-25 14:56 1961在Linux下查看内存我们 ... -
Linux编程C++内存管理之内存分配详解
2009-12-21 21:33 1727程序员们经常编写内存 ... -
内核模块调试方法
2009-11-17 11:23 2533对 于任何一位内核代 ... -
Linux操作系统下以太网卡的安装及配置
2009-11-11 17:18 1641Linux操作系统以其 独有的开放性、稳定性、高 ... -
跟我一起写 Makefile(3)
2009-11-03 14:38 604“-s” “--silent” “--quiet” 在命 ... -
跟我一起写 Makefile(2)
2009-11-03 14:35 928书写命令 ———— 每 ... -
跟我一起写 Makefile(1)
2009-11-03 14:32 950陈皓 (CSDN) 概述 — ... -
CC与GCC的区别概括介绍
2009-11-03 14:29 1421从名字上看,老的unix系统 的CC程序叫做C Comp ... -
linux内核模块管理命令
2009-10-30 10:54 16781. lsmod 列出已经加载的内核模块 lsmo ... -
分区修复软件Testdisk 安装使用
2009-08-26 14:23 4839linux下超帅的分区表修复软件.以前用过n多的window ... -
Linux下的分区修复软件Testdisk
2009-08-26 10:40 1812好了,这个软件叫testdisk.很帅的。。 如果你是使 ... -
Linux分区数据恢复
2009-08-26 10:18 1416公司的一台redhat机器启动不了了,是由于多次非正常关机造成 ... -
重装grub的方法及GRUB相关配置
2009-08-26 09:34 1570**当我们每次要重装WIN ...
相关推荐
线程如何去创建 ,多线程如何控制输出!互斥控制!
三个多线程经典代码只需要5分 转别人的,没分下载了 混点分,请多多支持
linux下C语言多线程编程实例.pdf
linux下C语言多线程编程实例.docx
linux下C语言多线程编程实例.doc
linux下C语言多线程编程实例[归纳].pdf
linux c 开发中多线程的典型实例,可在linux平台直接运行,通过实践熟悉理解多线程工作原理。
linux下C语言多线程编程实例 在单线程的程序里,有两种基本的数据:全局变量和局部变量。但在多线程程序里,还有第三种数据类型:线程数据(TSD: Thread-Specific Data)
linux下的C\C++多进程多线程编程实例详解 1、多进程编程 #include #include #include <unistd> int main() { pid_t child_pid; /* 创建一个子进程 */ child_pid = fork(); if(child_pid == 0) { printf...
主要介绍了linux系统中的c多线程编程实例,大家可以参考使用以下代码
在Linux下, 线程的互斥量数据类型是pthread_mutex_t. 在使用前, 要对它进行初始化: 对于静态分配的互斥量, 可以把它设置为PTHREAD_MUTEX_INITIALIZER, 或者调用pthread_mutex_init. 对于动态分配的互斥量, 在申请...
18、Linux网络编程18——tcp并发服务器(多线程) 19、Linux网络编程——tcp高效并发服务器(select实现) 20、Linux网络编程——tcp高效并发服务器(poll实现) 21、Linux网络编程——tcp高效并发服务器(epoll实现...
Linux下基于C/C++的Socket的阻塞和异步编程实例