原文:Linux C 多线程

linux下C语言多线程编程

#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10
pthread_t thread[];
pthread_mutex_t mut;
int number=, i;
void *thread1()
{
printf ("thread1 : I'm thread 1\n");
for (i = ; i < MAX; i++)
{
printf("thread1 : number = %d\n",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
Sleep();
}
printf("thread1 :主函数在等我完成任务吗?\n");
pthread_exit(NULL);
}
void *thread2()
{
printf("thread2 : I'm thread 2\n");
for (i = ; i < MAX; i++)
{
printf("thread2 : number = %d\n",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
Sleep();
}
printf("thread2 :主函数在等我完成任务吗?\n");
pthread_exit(NULL);
}
void thread_create(void)
{
int temp;
memset(&thread, , sizeof(thread)); //comment1
/*创建线程*/
if((temp = pthread_create(&thread[], NULL, thread1, NULL)) != ) //comment2
printf("线程1创建失败!\n");
else
printf("线程1被创建\n");
if((temp = pthread_create(&thread[], NULL, thread2, NULL)) != ) //comment3
printf("线程2创建失败");
else
printf("线程2被创建\n");
}
void thread_wait(void)
{
/*等待线程结束*/
if(thread[] !=) { //comment4
pthread_join(thread[],NULL);
printf("线程1已经结束\n");
}
if(thread[] !=) { //comment5
pthread_join(thread[],NULL);
printf("线程2已经结束\n");
}
}
int main()
{
/*用默认属性初始化互斥锁*/
pthread_mutex_init(&mut,NULL);
printf("我是主函数哦,我正在创建线程,呵呵\n");
thread_create();
printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n");
thread_wait();
return ;
}

执行结果

我是主函数哦,我正在创建线程,呵呵
线程1被创建
线程2被创建
我是主函数哦,我正在等待线程完成任务阿,呵呵
thread1 : I'm thread 1
thread1 : number =
thread2 : I'm thread 2
thread2 : number =
thread1 : number =
thread2 : number =
thread1 : number =
thread2 : number =
thread1 : number =
thread1 : number =
thread2 : number =
thread1 : number =
thread2 : number =
thread1 :主函数在等我完成任务吗?
线程1已经结束
thread2 :主函数在等我完成任务吗?
线程2已经结束

下面一个稍微复杂的多线程

extern int pthread_join __P ((pthread_t __th, void **__thread_return));
  第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的线程将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是象我们上面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。它的函数原型为:
  extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
  唯一的参数是函数的返回代码,只要pthread_exit中的参数retval不是NULL,这个值将被传递给 thread_return。最后要说明的是,一个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余调用pthread_join的线程则返回错误代码ESRCH。

实例:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
pthread_t tid1, tid2;
void *tret; void *
thr_fn1(void *arg)
{
sleep();//睡眠一秒,等待TID2结束。
pthread_join(tid2, &tret);//tid1一直阻赛,等到tid2的退出,获得TID2的退出码
printf("thread 2 exit code %d\n", (int)tret);
printf("thread 1 returning\n");
return((void *));
}
void *
thr_fn2(void *arg)
{
printf("thread 2 exiting\n");
pthread_exit((void *));
}
int
main(void)
{
int err;
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
if (err != )
printf("can't create thread 1\n");
err = pthread_create(&tid2, NULL, thr_fn2, NULL);
if (err != )
printf("can't create thread 2\n");
err = pthread_join(tid1, &tret);//祝线程一直阻赛,等待TID1的返回。
if (err != )
printf("can't join with thread 1\n");
printf("thread 1 exit code %d\n", (int)tret);
//err = pthread_join(tid2, &tret);
//if (err != 0)
// printf("can't join with thread 2\n");
// printf("thread 2 exit code %d\n", (int)tret);
exit();
} 命令:#gcc -lthread myfile11-.c
:#./a.out
运行结果:
thread exiting
thread exit code
thread returning
thread exit code

Linux C 多线程的更多相关文章

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

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

  2. 转载自~浮云比翼:Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥)

    Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥)   介绍:什么是线程,线程的优点是什么 线程在Unix系统下,通常被称为轻量级的进程,线程虽然不是进程,但却可 ...

  3. Linux 的多线程编程的高效开发经验(转)

    http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...

  4. Linux 的多线程编程的高效开发经验

    http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...

  5. Linux下多线程编程遇到的一些问题

    今天在学习了Linux的多线程编程的基础的知识点.于是就试着做了一个简单的Demo.本以为会得到预期的结果.不成想却遇到了意想不到的问题. 代码展示 我的C 代码很简单,就是一个简单的示例程序,如下: ...

  6. [转载]解决linux 下多线程错误 undefined reference to `sem_init'

    转自:https://blog.csdn.net/yzycqu/article/details/7396498?utm_source=copy 解决linux 下多线程错误 undefined ref ...

  7. linux C 多线程/线程池编程 同步实例

    在多线程.线程池编程中经常会遇到同步的问题. 1.创建线程 函数原型:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, ...

  8. Linux内核多线程实现方法 —— kthread_create函数【转】

    转自:http://blog.csdn.net/sharecode/article/details/40076951 Linux内核多线程实现方法 —— kthread_create函数 内核经常需要 ...

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

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

随机推荐

  1. 小说mvvm

    与多样化和复杂的前,设计模式不再是后端专有名词.从最初的面向对象的,框架制定了到现在为止mvc等一下,今天,它主要是关于Model-View-ViewModel(MVVM). 对于mvc大家可能都会相 ...

  2. Bean Validation 技术规范特性概述

    概述 Bean Validation 规范 Bean 是 Java Bean 的缩写.在 Java 分层架构的实际应用中,从表示层到持久化层.每一层都须要对 Java Bean 进行业务符合性验证,如 ...

  3. NUnit3 Test Adapter vs2015

    NUnit的安装 前言:NUnit是什么? NUnit 是一个单元测试框架,专门针对于.NET来写的.NUnit是xUnit家族种的第4个主打产品,完全由C#语言来编写,并且编写时充分利用了许多.NE ...

  4. boostrap-非常好用但是容易让人忽略的地方------Font Awesome

    font-awesome基本用法 官方代码传送门 font-awesome在bootstrap中的特殊用法(这个才是重点) 要点归纳1(官方) 官方代码传送门 要点归纳2(我的) <a href ...

  5. 【Linux&amp;Unix--文件描述叙事的性格和权柄】

    个人学习整理,如有不足之处,请不吝不吝赐教.转载请注明:@CSU-Max 系列博文:                      Linux&Unix学习第一弹 -- 文件描写叙述符与权限  L ...

  6. poj 1004 Dividing

    大意是,从输入六个数 .第i个数代表价值为i的有几个,平均分给两个人 ,明摆着的背包问题,本来以为把他转化为01背包.可是TLe,后来发现是12万的平方还多,所以妥妥的TLE,后来发现这是一个全然背包 ...

  7. php形式的内容被处理

    /** * 过滤HTML内容RETURN * * @param $string * @param bool $html * * @return array|string */ public stati ...

  8. Spring3.0学习笔记文档的官方网站(六)--3.4.1

    3.4 依靠 3.4.1 依赖注入     依赖注入两种方式:基于构造函数DI.基于setter方法DI. 3.4.1.1 基于构造函数DI     参数是引进一个对象的.和缺乏父母之前-子类关系: ...

  9. android使用ffmpeg

    cygwin上文编译文章. 在ffmpeg/arm添加的文件夹Android.mk 的主要目的是为了宣布动态库libs下一个 LOCAL_PATH:= $(call my-dir) include $ ...

  10. 1711 Number Sequence(kmp)

    Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...