/*************************************************************************
> 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. linux 中 chmod/chown/cngrp的用法与区别

      1.chgrp(转变文件所属用户组) chgrp 用户组 文件名 .若是整个目次下的都改,则加-R参数用于递归. 如:chgrp -R user smb.conf 2.chown(转变文件拥有者) ...

  2. Matlab 矩阵卷积理解(转载)

    转载自:http://blog.csdn.net/andrewseu/article/details/51783181 在图像处理的过程中,经常会看到矩阵卷积的概念,比如说用一个模板去和一张图片进行卷 ...

  3. 递归神经网络(RNN,Recurrent Neural Networks)和反向传播的指南 A guide to recurrent neural networks and backpropagation(转载)

    摘要 这篇文章提供了一个关于递归神经网络中某些概念的指南.与前馈网络不同,RNN可能非常敏感,并且适合于过去的输入(be adapted to past inputs).反向传播学习(backprop ...

  4. tomcat实现文件打开下载功能

    omcat作为http的下载服务器,网上有很多办法 但我认为最简单的是: 1.直接把文件放在 tomcat6/webapps/ROOT 目录下, 2.然后在网址中访问: http://192.168. ...

  5. STL概述

    一.关于STL STL(Standard Template Library,标准模板库)是C++语言标准中的重要组成部分.STL 以模板类和模板函数的形式为程序员提供了各种数据结构和算法的精巧实现,程 ...

  6. python study - 正则表达式

    第 7 章 正则表达式 7.1. 概览 7.2. 个案研究:街道地址 7.3. 个案研究:罗马字母 7.3.1. 校验千位数 7.3.2. 校验百位数 7.4. 使用 {n,m} 语法 7.4.1. ...

  7. linux下shell显示-bash-4.1#不显示路径解决方法

    在linux shell中不显示路径了,显示为-bash-4.1#用起来很不方便. 如何改为显示路径的shell呢? 步骤如下: vim ~/.bash_profile (不用管.bash_profi ...

  8. linux包之sysstat之sar命令

    要启动SAR,必须通过cron工具以周期性的间隔启动.安装sysstat包后,默认创建一个/etc/cron.d/sysstat文件,其默认内容为:# run system activity acco ...

  9. xml学习笔记二(规则)

    XML 的语法规则很简单,且很有逻辑.这些规则很容易学习,也很容易使用. 所有 XML 元素都须有关闭标签 在 HTML,经常会看到没有关闭标签的元素: <p>This is a para ...

  10. jquery点击改变图片src源码并toggle

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...