/*
* 对于线程之间的操作:
* 一个进程中会有多个线程,各个线程之间的或向切换会付出很小的代价相比于进程之间的切换
* 为什么要引入多线程:
* 1.与进程相比,他是一种非常节俭的多任务的操作方式。文们知道,在linux系统下,启动一个新的进程,
* 必须分配一个独立的地址空间,建立众多的数据表来维护它的代码段,堆栈段和数据段,这是一种开销和维护成本比较大的多任务工作方式。
* 而运行一个进程中的多个线程,他们彼此之间使用相同的地址空间,共享大部分的数据,启动一个线程花费的时间和空间远小于启动一个进程
* 所花费的空间;
* 2.线程间的通信比较方便,对于不同的进程来说,他们具有独立的数据空间,要进行数据传递只能以通信的方式进行,这种方式不仅费时,而且不方便
* 线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其他线程所用。
* 3.提高应用程序的响应;
* 4.使多CPU系统更加有效,操作系统会保证当线程数不大于CPU数目时,不同的线程运行在不同的CPU上
* 5.改善程序结构,一个即长有复杂的进程,可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样程序便于理解和修改
* */ #include <pthread.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/resource.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++)
{
pthread_mutex_lock(&mut);
printf("thread1 : number = %d\n",number);
number++;
pthread_mutex_unlock(&mut);
msleep(1000*100);
}
printf("thread1 :wait the main thread to finish the task?\n");
pthread_exit(NULL);
} void * thread2(void * )
{
printf("thread2 : I'm thread 2\n");
for(i=0; i<MAX; i++)
{
pthread_mutex_lock(&mut);
printf("thread2 : number = %d\n",number);
number--;
pthread_mutex_unlock(&mut);
msleep(1000*100);
}
printf("thread2 :wait the main thread to finish the task?\n");
pthread_exit(NULL);
} void thread_create(void)
{
int temp;
memset(&thread,0,sizeof(thread));
temp = pthread_create(&thread[0],NULL,thread1,NULL);
if(temp != 0)
printf("create thread 1 failed\n");
else
printf("create thread 1 successful\n"); temp = pthread_create(&thread[1],NULL,thread2,NULL);
if(temp != 0)
printf("create thread 2 failed\n");
else
printf("create thread 2 successful\n");
} void thread_wait(void)
{
if(thread[0] != 0)
{
pthread_join(thread[0],NULL);
printf("thread 1 has finished \n");
}
if(thread[1] != 0)
{
pthread_join(thread[0],NULL);
printf("thread 2 has finished \n");
}
} int main()
{
pthread_mutex_init(&mut,NULL);
printf("I am the main thread\n");
thread_create();
printf("Main thread wait other thread run\n");
thread_wait();
return 0;
}

在我笔记本上的运行结果是:

I am the main thread
create thread 1 successful
create thread 2 successful
Main thread wait other thread run
thread2 : I'm thread 2
thread1 : I'm thread 1
thread2 : number = 0
thread1 : number = -1
thread2 : number = 0
thread1 : number = -1
thread2 : number = 0
thread1 : number = -1
thread1 : number = 0
thread2 : number = 1
thread2 : number = 0
thread1 : number = -1
thread2 : number = 0
thread1 : number = -1
thread1 : number = 0
thread2 : number = 1
thread2 :wait the main thread to finish the task?
thread1 :wait the main thread to finish the task?
thread 1 has finished
thread 2 has finished

linux 中多线程使用的更多相关文章

  1. linux中多线程解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  2. Linux中多线程信号的处理

    1. 博文:Linux多线程中使用信号-1  http://blog.csdn.net/qq276592716/article/details/7325250 2. 博文:Linux多线程信号总结  ...

  3. 【转】Linux中多线程wait使用注意

    使用管道生成的while,是无法进行并发管理的 在处理日志的时候,采用管道多线程,怎么都实现不了wait功能,经上篇文章才知道,使用管道生成的while,无法进行并发管理. while read qu ...

  4. Linux中线程的挂起与恢复(进程暂停)

    http://www.linuxidc.com/Linux/2013-09/90156.htm 今天在网上查了一下Linux中对进程的挂起与恢复的实现,相关资料少的可怜,大部分都是粘贴复制.也没有完整 ...

  5. Linux中查看进程的多线程

    在SMP系统中,我们的应用程序经常使用多线程的技术,那么在Linux中如何查看某个进程的多个线程呢? 本文介绍3种命令来查看Linux系统中的线程(LWP)的情况: 在我的系统中,用qemu-syst ...

  6. Linux中查看进程的多线程pstree, ps -L

    Linux中查看进程的多线程 在SMP系统中,我们的应用程序经常使用多线程的技术,那么在Linux中如何查看某个进程的多个线程呢? 本文介绍3种命令来查看Linux系统中的线程(LWP)的情况:在我的 ...

  7. linux 中 eclipse 开发 c/c++ 多线程程序,添加 libpthread.a 库支持

    导入头文件 在 linux 中开发多线程程序,在使用到 pthread 系列函数的文件中,需要导入头文件: #include <pthread.h> 链接 libpthread.a 在编译 ...

  8. linux中c多线程同步方法

    https://blog.csdn.net/jkx01whg/article/details/78119189 Linux下提供了多种方式来处理线程同步,最常用的是互斥锁.条件变量和信号量. 一.互斥 ...

  9. [转]Linux 的多线程编程的高效开发经验

    Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微和隐晦的差别.不注意这些 Linux 上的一些开发陷阱,常常会导致程序问题不穷,死锁不断.本文中我们 ...

随机推荐

  1. JDK,JRE,JVM区别与联系(转)

    JDK : JavaDevelopment ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具 ...

  2. jar文件につぃて

    打包jar文件和设置class路径: 查看jar文件内容:

  3. Eclipse中搭建Python开发环境

    转载:http://www.cnblogs.com/realh/archive/2010/10/04/1841907.html Eclipse+PyDev环境搭建 1.准备工作 JDK6 Java 开 ...

  4. SQL Server 内存不足引起的并发症

    第一:cpu 1.内存不足就会有频繁的页面调入调出.这个过程是要有cpu的参与的.所以这个要影响cpu! 2.内存不足可能会引有用起执行计划被清除.当起次要执行时.这个就要重编译一次!

  5. Mysql 权限修改何时生效

    首先权限是记录在表中的,所以如果我们要修改权限只要修改表中的数据就可以了! 方法 1 grant ,revoke,set password,rename user .......等等 2 insert ...

  6. slave 成为master 时候执行的操作notify_master /etc/keepalived/send_master.sh

    slave:/root# cat /etc/keepalived/keepalived.conf global_defs { router_id MySQL-ha } vrrp_instance VI ...

  7. [ javascript ] 司徒正美的fadeOut-fadeIn效果!

    首先感谢司徒正美的文章! 在司徒大神的博客看到一个简单的渐入渐出的效果.全然採用js实现. 例如以下: <!doctype html> <html dir="ltr&quo ...

  8. HDU 1002 A + B Problem II(大整数相加)

    A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  9. o怎么样racle输入dmp数据库文件

    Oracle进出口数据imp/exp等价物oracle数据恢复和备份. exp命令可以从远程数据库传输数据server出到本地的dmp文件,imp命令能够把dmp文件从本地导入到远处的数据库serve ...

  10. linux 下手动编译安装无线网卡驱动

    先参照 <本地yum源安装GCC >安装好gcc hp的笔记本上安装了CentOS6.3,没有安装无线网卡驱动,安装这个驱动,在Google上找了好多资料,最后终于解决了这个问题.在这里做 ...