linux中多线程解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread。编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a。顺便说一下,Linux 下pthread的实现是通过系统调用clone()来实现的。clone()是 Linux所特有的系统调用,它的使用方式类似fork,关于clone()的详细情况,有兴趣的读者可以去查看有关文档说明。下面我们展示一个最简单的多线程程序 pthread_create.c。
一个重要的线程创建函数原型:
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr, void *(*start_rtn)(void),void *restrict arg);
返回值:若是成功建立线程返回0,否则返回错误的编号
形式参数:
pthread_t *restrict tidp 要创建的线程的线程id指针
const pthread_attr_t *restrict attr 创建线程时的线程属性
void* (start_rtn)(void) 返回值是void类型的指针函数
void *restrict arg start_rtn的行参
例程1:
功能:创建一个简单的线程
程序名称:pthread_create.c
/********************************************************************************************
** Name:pthread_create.c
** Used to study the multithread programming in Linux OS
** Author:zeickey
** Date:2006/9/16
** Copyright (c) 2006,All Rights Reserved!
*********************************************************************************************/
#include <stdio.h>
#include <pthread.h>
void *myThread1(void)
{
int i;
for (i=0; i<100; i++)
{
printf("This is the 1st pthread,created by zieckey./n");
sleep(1);//Let this thread to sleep 1 second,and then continue to run
}
}
void *myThread2(void)
{
int i;
for (i=0; i<100; i++)
{
printf("This is the 2st pthread,created by zieckey./n");
sleep(1);
}
}
int main()
{
int i=0, ret=0;
pthread_t id1,id2;
ret = pthread_create(&id2, NULL, (void*)myThread1, NULL);
if (ret)
{
printf("Create pthread error!/n");
return 1;
}
ret = pthread_create(&id2, NULL, (void*)myThread2, NULL);
if (ret)
{
printf("Create pthread error!/n");
return 1;
}
pthread_join(id1, NULL);
pthread_join(id2, NULL);
return 0;
}
我们编译此程序:
# gcc pthread_create.c -lpthread
因为pthread的库不是linux系统的库,所以在进行编译的时候要加上-lpthread,否则编译不过,会出现下面错误
thread_test.c: 在函数 ‘create’ 中:
thread_test.c:7: 警告: 在有返回值的函数中,程序流程到达函数尾
/tmp/ccOBJmuD.o: In function `main':thread_test.c:(.text+0x4f):对‘pthread_create’未定义的引用
collect2: ld 返回 1
运行,我们得到如下结果:
# ./a.out
This is the 1st pthread,created by zieckey.
This is the 2st pthread,created by zieckey.
This is the 1st pthread,created by zieckey.
This is the 2st pthread,created by zieckey.
This is the 2st pthread,created by zieckey.
This is the 1st pthread,created by zieckey.
....
两个线程交替执行。
此例子介绍了创建线程的方法。
下面例子介绍向线程传递参数。
例程2:
功能:向新的线程传递整形值
程序名称:pthread_int.c
/********************************************************************************************
** Name:pthread_int.c
** Used to study the multithread programming in Linux OS
** Pass a parameter to the thread.
** Author:zeickey
** Date:2006/9/16
** Copyright (c) 2006,All Rights Reserved!
*********************************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *create(void *arg)
{
int *num;
num=(int *)arg;
printf("create parameter is %d /n",*num);
return (void *)0;
}
int main(int argc ,char *argv[])
{
pthread_t tidp;
int error;
int test=4;
int *attr=&test;
error=pthread_create(&tidp,NULL,create,(void *)attr);
if(error)
{
printf("pthread_create is created is not created ... /n");
return -1;
}
sleep(1);
printf("pthread_create is created .../n");
return 0;
}
编译方法:
gcc -lpthread pthread_int.c -Wall
执行结果:
create parameter is 4
pthread_create is created is created ...
例程总结:
可以看出来,我们在main函数中传递的整行指针,传递到我们新建的线程函数中。
在上面的例子可以看出来我们向新的线程传入了另一个线程的int数据,线程之间还可以传递字符串或是更复杂的数据结构。
例程3:
程序功能:向新建的线程传递字符串
程序名称:pthread_string.c
/********************************************************************************************
** Name:pthread_string.c
** Used to study the multithread programming in Linux OS
** Pass a ‘char*‘ parameter to the thread.
** Author:zeickey
** Date:2006/9/16
** Copyright (c) 2006,All Rights Reserved!
*********************************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *create(void *arg)
{
char *name;
name=(char *)arg;
printf("The parameter passed from main function is %s /n",name);
return (void *)0;
}
int main(int argc, char *argv[])
{
char *a="zieckey";
int error;
pthread_t tidp;
error=pthread_create(&tidp, NULL, create, (void *)a);
if(error!=0)
{
printf("pthread is not created./n");
return -1;
}
sleep(1);
printf("pthread is created... /n");
return 0;
}
编译方法:
gcc -Wall pthread_string.c -lpthread
执行结果:
The parameter passed from main function is zieckey
pthread is created...
例程总结:
可以看出来main函数中的字符串传入了新建的线程中。
例程4:
程序功能:向新建的线程传递字符串
程序名称:pthread_struct.c
/********************************************************************************************
** Name:pthread_struct.c
** Used to study the multithread programming in Linux OS
** Pass a ‘char*‘ parameter to the thread.
** Author:zeickey
** Date:2006/9/16
** Copyright (c) 2006,All Rights Reserved!
*********************************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
struct menber
{
int a;
char *s;
};
void *create(void *arg)
{
struct menber *temp;
temp=(struct menber *)arg;
printf("menber->a = %d /n",temp->a);
printf("menber->s = %s /n",temp->s);
return (void *)0;
}
int main(int argc,char *argv[])
{
pthread_t tidp;
int error;
struct menber *b;
b=(struct menber *)malloc( sizeof(struct menber) );
b->a = 4;
b->s = "zieckey";
error = pthread_create(&tidp, NULL, create, (void *)b);
if( error )
{
printf("phread is not created.../n");
return -1;
}
sleep(1);
printf("pthread is created.../n");
return 0;
}
编译方法:
gcc -Wall pthread_struct.c -lpthread
执行结果:
menber->a = 4
menber->s = zieckey
pthread is created...
例程总结:
可以看出来main函数中的一个结构体传入了新建的线程中。
线程包含了标识进程内执行环境必须的信息。他集成了进程中的所有信息都是对线程进行共享的,包括文本程序、程序的全局内存和堆内存、栈以及文件描述符。
例程5:
程序目的:验证新建立的线程可以共享进程中的数据
程序名称:pthread_share.c
/********************************************************************************************
** Name:pthread_share_data.c
** Used to study the multithread programming in Linux OS
** Pass a ‘char*‘ parameter to the thread.
** Author:zeickey
** Date:2006/9/16
** Copyright (c) 2006,All Rights Reserved!
*********************************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
static int a=4;
void *create(void *arg)
{
printf("new pthread ... /n");
printf("a=%d /n",a);
return (void *)0;
}
int main(int argc,char *argv[])
{
pthread_t tidp;
int error;
a=5;
error=pthread_create(&tidp, NULL, create, NULL);
if(error!=0)
{
printf("new thread is not create ... /n");
return -1;
}
sleep(1);
printf("new thread is created ... /n");
return 0;
}
编译方法:
gcc -Wall pthread_share_data.c -lpthread
执行结果:
new pthread ...
a=5
new thread is created ...
例程总结:
可以看出来,我们在主线程更改了我们的全局变量a的值的时候,我们新建立的线程则打印出来了改变的值,可以看出可以访问线程所在进程中的数据信息。
2、线程的终止
如果进程中任何一个线程中调用exit,_Exit,或者是_exit,那么整个进程就会终止,
与此类似,如果信号的默认的动作是终止进程,那么,把该信号发送到线程会终止进程。
线程的正常退出的方式:
(1) 线程只是从启动例程中返回,返回值是线程中的退出码
(2) 线程可以被另一个进程进行终止
(3) 线程自己调用pthread_exit函数
两个重要的函数原型:
#include <pthread.h>
void pthread_exit(void *rval_ptr);
/*rval_ptr 线程退出返回的指针*/
int pthread_join(pthread_t thread,void **rval_ptr);
/*成功结束进程为0,否则为错误编码*/
例程6
程序目的:线程正常退出,接受线程退出的返回码
程序名称:pthread_exit.c
/********************************************************************************************
** Name:pthread_exit.c
** Used to study the multithread programming in Linux OS
** A example showing a thread to exit and with a return code.
** Author:zeickey
** Date:2006/9/16
** Copyright (c) 2006,All Rights Reserved!
*********************************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *create(void *arg)
{
printf("new thread is created ... /n");
return (void *)8;
}
int main(int argc,char *argv[])
{
pthread_t tid;
int error;
void *temp;
error = pthread_create(&tid, NULL, create, NULL);
if( error )
{
printf("thread is not created ... /n");
return -1;
}
error = pthread_join(tid, &temp);
if( error )
{
printf("thread is not exit ... /n");
return -2;
}
printf("thread is exit code %d /n", (int )temp);
return 0;
}
编译方法:
gcc -Wall pthread_exit.c -lpthread
执行结果:
new thread is created ...
thread is exit code 8
例程总结:
可以看出来,线程退出可以返回线程的int数值。线程退出不仅仅可以返回线程的int数值,还可以返回一个复杂的数据结构。
例程7
程序目的:线程结束返回一个复杂的数据结构
程序名称:pthread_return_struct.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
struct menber
{
int a;
char *b;
}temp={8,"zieckey"};
void *create(void *arg)
{
printf("new thread ... /n");
return (void *)&temp;
}
int main(int argc,char *argv[])
{
int error;
pthread_t tid;
struct menber *c;
error = pthread_create(&tid, NULL, create, NULL);
if( error )
{
printf("new thread is not created ... /n");
return -1;
}
printf("main ... /n");
error = pthread_join(tid,(void *)&c);
if( error )
{
printf("new thread is not exit ... /n");
return -2;
}
printf("c->a = %d /n",c->a);
printf("c->b = %s /n",c->b);
sleep(1);
return 0;
}
编译方法:
gcc -Wall pthread_return_struct.c -lpthread
执行结果:
main ...
new thread ...
c->a = 8
c->b = zieckey
例程总结:
一定要记得返回的数据结构要是在这个数据要返回的结构没有释放的时候应用,
如果数据结构已经发生变化,那返回的就不会是我们所需要的,而是脏数据
3、线程标识
函数原型:
#include <pthread.h>
pthread_t pthread_self(void);
pid_t getpid(void);
getpid()用来取得目前进程的进程识别码,函数说明
例程8
程序目的:实现在新建立的线程中打印该线程的id和进程id
程序名称:pthread_id.c
/********************************************************************************************
** Name:pthread_id.c
** Used to study the multithread programming in Linux OS.
** Showing how to get the thread's tid and the process's pid.
** Author:zeickey
** Date:2006/9/16
** Copyright (c) 2006,All Rights Reserved!
*********************************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h> /*getpid()*/
void *create(void *arg)
{
printf("New thread .... /n");
printf("This thread's id is %u /n", (unsigned int)pthread_self());
printf("The process pid is %d /n",getpid());
return (void *)0;
}
int main(int argc,char *argv[])
{
pthread_t tid;
int error;
printf("Main thread is starting ... /n");
error = pthread_create(&tid, NULL, create, NULL);
if(error)
{
printf("thread is not created ... /n");
return -1;
}
printf("The main process's pid is %d /n",getpid());
sleep(1);
return 0;
}
编译方法:
gcc -Wall -lpthread pthread_id.c
执行结果:
Main thread is starting ...
The main process's pid is 3307
New thread ....
This thread's id is 3086347152
The process pid is 3307
介绍linux线程的基本概念,线程间的互斥和同步机制,分析了linuxpthread库的API函数,并结合一个例子阐述多线程编程的核心技术,最后总结出多线程编程应注意的事项。
#include<stdio.h>
#include<pthread.h>
#define BUFFER_SIZE 8
struct prodcons {
int buffer[BUFFER_SIZE];
pthread_mutex_t lock; //互斥LOCK
int readpos , writepos;
pthread_cond_t notempty; //缓冲区非空条件判断
pthread_cond_t notfull; //缓冲区未满条件判断
};
void init(struct prodcons * b){
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->notempty,NULL);
pthread_cond_init(&b->notfull,NULL);
b->readpos=0;
b->writepos=0;
}
void put(struct prodcons* b,int data){
pthread-_mutex_lock(&b->lock);
if((b->writepos + 1) % BUFFER_SIZE == b->readpos)
{
pthread_cond_wait(&b->notfull, &b->lock) ;
}
b->buffer[b->writepos]=data;
b->writepos++;
if(b->writepos >= BUFFER_SIZE)
b->writepos=0;
pthread_cond_signal(&b->notempty);
pthread_mutex_unlock(&b->lock);
}
int get(struct prodcons *b){
int data;
pthread_mutex_lock(&b->lock);
if(b->writepos == b->readpos)
{
pthread_cond _wait(&b->notempty, &b->lock);
}
data = b->buffer[b->readpos];
b->readpos++;
if(b->readpos >= BUFFER_SIZE)
b->readpos=0;
pthread_cond_signal(&b->notfull);
pthread_mutex_unlock(&b->lock);
return data;
}
#define OVER (-1)
struct prodcons buffer;
void *producer(void *data)
{
int n;
for(n = 0; n < 10000; n++)
{
printf("%d \n", n) ;
put(&buffer, n);
}
put(&buffer, OVER);
return NULL;
}
void *consumer(void * data)
{
int d;
while(1)
{
d = get(&buffer);
if(d == OVER)
break;
printf("%d\n", d);
}
return NULL;
}
int main(void)
{
pthread_t th_a, th_b;
void *retval;
init(&buffer);
pthread_create(&th_a, NULL, producer, 0);
pthread_create(&th_b, NULL, consumer, 0);
pthread_join(th_a, &retval);
pthread_join(th_b, &retval);
return 0;
}
linux中多线程解析的更多相关文章
- Linux中多线程信号的处理
1. 博文:Linux多线程中使用信号-1 http://blog.csdn.net/qq276592716/article/details/7325250 2. 博文:Linux多线程信号总结 ...
- linux 中多线程使用
/* * 对于线程之间的操作: * 一个进程中会有多个线程,各个线程之间的或向切换会付出很小的代价相比于进程之间的切换 * 为什么要引入多线程: * 1.与进程相比,他是一种非常节俭的多任务的操作方式 ...
- 【转】Linux中多线程wait使用注意
使用管道生成的while,是无法进行并发管理的 在处理日志的时候,采用管道多线程,怎么都实现不了wait功能,经上篇文章才知道,使用管道生成的while,无法进行并发管理. while read qu ...
- Linux中线程的挂起与恢复(进程暂停)
http://www.linuxidc.com/Linux/2013-09/90156.htm 今天在网上查了一下Linux中对进程的挂起与恢复的实现,相关资料少的可怜,大部分都是粘贴复制.也没有完整 ...
- Linux中查看进程的多线程
在SMP系统中,我们的应用程序经常使用多线程的技术,那么在Linux中如何查看某个进程的多个线程呢? 本文介绍3种命令来查看Linux系统中的线程(LWP)的情况: 在我的系统中,用qemu-syst ...
- Linux中查看进程的多线程pstree, ps -L
Linux中查看进程的多线程 在SMP系统中,我们的应用程序经常使用多线程的技术,那么在Linux中如何查看某个进程的多个线程呢? 本文介绍3种命令来查看Linux系统中的线程(LWP)的情况:在我的 ...
- 解析Linux中的VFS文件系统机制
转载:原文地址https://www.ibm.com/developerworks/cn/linux/l-vfs/ 1. 摘要 本文阐述 Linux 中的文件系统部分,源代码来自基于 IA32 的 2 ...
- linux 中解析命令行参数(getopt_long用法)
linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...
- Java中多线程的使用(超级超级详细)线程安全原理解析 4
Java中多线程的使用(超级超级详细)线程安全 4 什么是线程安全? 有多个线程在同时运行,这些线程可能会运行相同的代码,程序运行的每次结果和单线程运行的结果是一样的,而且其他变量的值也和预期的值一样 ...
随机推荐
- 深入浅出—JAVA(5)
5.编写程序
- openrisc 之 Wishbone总线学习笔记——接口信号定义
这部分内容就是copy下来的,网上到处都有.先看看接口啥样子,在详细说明 接口定义copy http://blog.csdn.net/ce123/article/details/6929897.百度文 ...
- Code 16K 码
Code 16K 码是一种多层.连续型.可变长度的条码符号,可以表示全ASCII字符集的128个字符及扩展ASCII字符.它采用UPC及Code128字符.一个16层的Code 16K符号,可以表示7 ...
- cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap(三)-按键处理
在地图初始化好了之后,就开始移动马里奥吧,我在windows下,是使用键盘来移动马里奥的 w是跳,d是前进,a是后退,那么在程序里是怎么来获取这个按键的呢? 普通的windows程序,在按键之后,会有 ...
- vs提示“当前不会命中断点,源代码与原始版本不同”的一种解决办法
将出问题的cpp文件用notepad打开,另存为‘unicode’类型,覆盖源文件即可
- HDU4648+Easy
N^2都能过!!!!!!! /* Easy */ #include<stdio.h> #include<string.h> #include<stdlib.h> # ...
- [置顶] 吃论扯谈---吃货和Office 365订阅的关系
什么事物都可以和吃联系起来,在女孩子穿裙子的季节这是一个悲伤的故事! 说明: :Office365是微软云计算产品之一,其采取订阅的方式,按人头*每月的方式付费,用户可以选择自己需要的服务 2:Off ...
- ThinkPHP - 模板使用函数
模板使用函数 1.模板引擎自带函数:仅仅是输出变量并不能满足模板输出的需要,内置模板引擎支持对模板变量使用调节器和格式化功能,其实也就是提供函数支持,并支持多个函数同时使用.用于模板标签的函数可以是P ...
- 循环调用修正sic86 2改后的(除了第一年有点诡异,其他年份可以正常修复)
create or replace procedure rebuild_sic86_wyl(pi_aac001 in number, po_fhz out varchar2, po_msg out v ...
- Pathchirp—有效的带宽估计方法(二)
上一个blog介绍了有效带宽估计方法:pathload.http://blog.csdn.net/ice110956/article/details/11126491. 做一个小小的总结:pathlo ...