原文: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. Java重写round()方法

    题目:完毕这种方法的代码实现 public static String round (String arg1, int arg2) 參数 arg1:表示等待被处理的数据:如:"100.286 ...

  2. StackExchange.Redis 使用-发布订阅 (二)

    使用Redis的发布订阅功能 redis另一个常见的用途是发布订阅功能 . 它非常的简单 ,当连接失败时 ConnectionMultiplexer 会自动重新进行订阅 . ISubscriber s ...

  3. gtest简短,简单易用

    gtest它是一种跨平台的(Liunx.Mac OS X.Windows.Cygwin.Windows CE and Symbian)的C++测试框架.有google该公司宣布. gtest台上为编写 ...

  4. Shell 文字 顶替tomcat星团war包裹

    执行前server造成免费ssh登陆. 修改配置文件不是太大 您可以覆盖原有的配置 profile 简介 bak=/home/wsm/bak webapps=/home/wsm/webapps/WSM ...

  5. 解决win10远程桌面没法关机问题

    win10远程桌面没法关机问题: 解决方法:alt+f4

  6. Think in Java(一):Java基础

    一. OOP的特点 (1) 万物皆为对象; (2) 程序是对象的集合,他们通过发送信息来告诉彼此所要做的; (3) 每一个对象都有自己的由其它对象所构成的存储; (4) 每一个对象都拥有它的类型; ( ...

  7. 连载:面向对象的葵花宝典:思考、技巧与实践(39) - 设计原则 vs 设计模式

    它的设计原则,和设计模式,是否该用它? ============================================================================= 在& ...

  8. atittit.表单验证性质的原则和实施,以及选择和定义自己的兼容easyui dsl窗体身份验证规则

    atittit.表单验证性质的原则和实施,以及选择和定义自己的兼容easyui dsl规则的表单验证 1. 需求,表单验证须要弹框式,但眼下easyui ms绑定死了tooltip式样 1 2. 表单 ...

  9. emacs quick open and jump file (or buffer) which name is current word

    Sometime, we need to open a file or buffer which name begin with current word in emacs. Here I give ...

  10. 百度官方wormHole后门检测记录(转)

    本次这个安卓后门真是哔了狗了,刚加入大安卓阵营就出了这个篓子,跟你root不root和什么安卓版本没有版毛钱关系,完全是百度SDK官方提供的后门,不是漏洞,人为的. 乌云地址:http://drops ...