/*************************************************************************
> File Name: thread1.c
> Author:
> Mail:
> Created Time: 2016年03月26日 星期六 22时37分44秒
************************************************************************/ #include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h> void *thread_function(void *arg);
char message[] = "hello world"; int main(){
int res;
pthread_t a_thread;
void *thread_result;
//pthread_create 开始运行多线程,如下所示
res = pthread_create(&a_thread,NULL,thread_function,(void*)message);
//先向pthrea_create传递了一个pthread_t类型对象的地址
//后面我们可以用它来引用这个新线程
//不想改变默认线程属性,第二个参数设置为NULL
//最后两个参数分别是将要调用的函数和一个传递给该函数的参数 if(res != ){
perror("thread creation failed");
exit(EXIT_FAILURE);
}
//如果成功,就有两个线程运行,
//1原来的线程,main继续执行pthread_create后面的代码
//2新线程开始执行thread_function函数
printf("Waiting for thread to finsh...\n"); res = pthread_join(a_thread, &thread_result);
//a_thread 正在等待其结束的线程的标识符
//thread_result,指向线程返回值的指针
//这个函数将等到它所指定的线程终止才返回,想想wait()的功能
if(res!=){
perror("thread join failed");
exit(EXIT_FAILURE);
}
//然后main打印新线程的返回值和全局变量message的值
//exit
printf("thread join, it returned %s\n",(char*)thread_result);
printf("Message is now %s\n",message);
exit(EXIT_SUCCESS);
} void *thread_function(void *arg){
printf("thread_function is running. Argument was %s\n",(char*)arg);
sleep();
//todo something
strcpy(message,"Bye!");
pthread_exit("thank you for the cpu time");
}

执行结果

lizhen@lizhen:~/basic$ ./thread1
Waiting for thread to finsh...
thread_function is running. Argument was hello world
thread join, it returned thank you for the cpu time
Message is now Bye!

======================

同时执行--

/*************************************************************************
> File Name: thread1.c
> Author:
> Mail:
> Created Time: 2016年03月26日 星期六 22时37分44秒
************************************************************************/ #include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h> void *thread_function(void *arg);
char message[] = "hello world";
int run_now = ; int main(){
int res;
pthread_t a_thread;
void *thread_result;
//pthread_create 开始运行多线程,如下所示
res = pthread_create(&a_thread,NULL,thread_function,(void*)message );
//先向pthrea_create传递了一个pthread_t类型对象的地址
//后面我们可以用它来引用这个新线程
//不想改变默认线程属性,第二个参数设置为NULL
//最后两个参数分别是将要调用的函数和一个传递给该函数的参数
int print_count1 = ;
while(print_count1++ < ){
if(run_now == ){
printf("");
run_now = ;
}else{
sleep();
}
} if(res != ){
perror("thread creation failed");
exit(EXIT_FAILURE);
}
//如果成功,就有两个线程运行,
//1原来的线程,main继续执行pthread_create后面的代码
//2新线程开始执行thread_function函数
printf("Waiting for thread to finsh...\n"); res = pthread_join(a_thread, &thread_result);
//a_thread 正在等待其结束的线程的标识符
//thread_result,指向线程返回值的指针
//这个函数将等到它所指定的线程终止才返回,想想wait()的功能
printf("thread joined\n");
if(res!=){
perror("thread join failed");
exit(EXIT_FAILURE);
}
//然后main打印新线程的返回值和全局变量message的值
//exit
//printf("thread join, it returned %s\n",(char*)thread_result);
// printf("Message is now %s\n",message);
printf("\n");
exit(EXIT_SUCCESS);
} void *thread_function(void *arg){
//todo something
int print_count2 = ;
while(print_count2++ < ){
if(run_now == ){
printf("");
run_now = ;
}else{
sleep();
}
}
printf("\n");
}

执行结果:

lizhen@lizhen:~/basic$ ./thread2

Waiting for thread to finsh...
thread joined lizhen@lizhen:~/basic$

============

同步

[linux basic]--线程的更多相关文章

  1. [linux basic 基础]----线程的属性

    在信号量和互斥量例子中,我们都是在程序推出之前利用pthread_join对线程进行再次同步:如果想让thread想创建它的线程返回数据我需要这么做:问题:我们有时候既不需要第二个线程向main线程返 ...

  2. Linux/Unix 线程同步技术之互斥量(1)

    众所周知,互斥量(mutex)是同步线程对共享资源访问的技术,用来防止下面这种情况:线程A试图访问某个共享资源时,线程B正在对其进行修改,从而造成资源状态不一致.与之相关的一个术语临界区(critic ...

  3. Linux获取线程tid线程名

    Linux获取线程tid线程名 1 2 3 4 5 6 //thread name char cThreadName[32] = {0}; prctl(PR_GET_NAME, (unsigned l ...

  4. Linux编程---线程

    首先说一下线程的概念.事实上就是运行在进程的上下文环境中的一个运行流.普通进程仅仅有一条运行流,可是线程提供了多种运行的路径并行的局面. 同一时候,线程还分为核心级线程和用户级线程.主要差别在属于核内 ...

  5. Linux 多线程 - 线程异步与同步机制

    Linux 多线程 - 线程异步与同步机制 I. 同步机制 线程间的同步机制主要包括三个: 互斥锁:以排他的方式,防止共享资源被并发访问:互斥锁为二元变量, 状态为0-开锁.1-上锁;开锁必须由上锁的 ...

  6. Linux 默认线程栈大小 调优

    Linux 线程栈介绍 栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清除的变量的存储区.里面的变量通常是局部变量.函数参数等:和堆相比,栈通常很小. Linux 查询线程栈 1.查看默认的 ...

  7. Linux内核线程创建

    本文旨在简单介绍一下Linux内核线程: 先举个例子: 不插U盘,在Linux命令行中输入:ps -el:然后插上U盘,再次输入:ps -el 会发现多出了下面一行(当然还会有其他的,比如scsi相关 ...

  8. Linux 下线程的理解

    2017-04-03 最近深入研究了下Linux线程的问题,发现自己之前一直有些许误解,特记之…… 关于Linux下的线程,各种介绍Linux的书籍都没有深入去解释的,或许真的如书上所述,Linux本 ...

  9. Linux中线程使用详解

    线程与进程为什么有了进程的概念后,还要再引入线程呢?使用多线程到底有哪些好处?什么的系统应该选用多线程?我们首先必须回答这些问题. 使用多线程的理由之一是和进程相比,它是一种非常"节俭&qu ...

随机推荐

  1. Matlab位运算操作

    本文为转载他人文章: bitand 按位与操作 a = 7; b = bitand(10,a); disp(dec2bin(a,8)); %ans = 00000111 disp(dec2bin(b, ...

  2. ZOJ 1047 Image Perimeters

    原题链接 题目大意:鼠标点击一块,求与之联通的所有区域的边长之和. 解法:广度优先搜索.从选中的这个点开始,往周围8个点依次搜索,访问过的点做上标记.如果该点上下左右的一个或多个方向没有相邻的点,边长 ...

  3. spring+websocket整合

    java-websocket的搭建非常之容易,没用框架的童鞋可以在这里下载撸主亲自调教好的java-websocket程序: Apach Tomcat 8.0.3+MyEclipse+maven+JD ...

  4. makefile--模式规则(七)

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 上一节讲到目录创建成功,目标文件没有生产到对应目录下,这里我们先给目标文件加上对应目录,这样的话 ...

  5. windows7下python3.4.3 添加库路径(转)

    1, 动态的添加库路径.在程序运行过程中修改sys.path的值,添加自己的库路径import syssys.path.append(r'your_path') 2, 在Python安装目录下的\Li ...

  6. Apache的虚拟主机配置

    使用虚拟主机要先取消中心主机,注释掉DocumentRoot #DocumentRoot "/www/htdoc" 虚拟主机的单独配置: 用户认证 访问日志 错误日志 别名 脚本别 ...

  7. 1-3-1 关于API

    主要内容:API函数及其相关内容的介绍.Windows编程相关基础知识介绍 1.API函数的概念 <1>API(Application Programming interface),即应用 ...

  8. oh-my-zsh的使用

    一.自动安装wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh 二.配置文件~/. ...

  9. 黑马程序员——JAVA基础之单列设计模式

    ------- android培训.java培训.期待与您交流! ---------- 单列设计模式是面试中的一个常考的点,所谓单例模式就是说对象在内存中只能存在一个.如果有其他变量是该类对象,那么他 ...

  10. UBUNTU下SUBLIME TEXT3的安装+破解+汉化+中文输入

    一.Sublime Text3的下载安装 建议直接去官网下载最新版deb安装包:http://www.sublimetext.com/3 二.Sublime Text3的破解 3114版 -– BEG ...