/*************************************************************************
> 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. SpringMVC4零配置--Web上下文配置【MvcConfig】

    与SpringSecurity的配置类似,spring同样为我们提供了一个实现类WebMvcConfigurationSupport和一个注解@EnableWebMvc以帮助我们减少bean的声明. ...

  2. 关于SSH整合中对于Hibernate的Session关闭的问题

    在web.xml的Struts2的配置上面加上 <filter> <filter-name>OpenSessionInViewFilter</filter-name> ...

  3. 兼容FF 加入收藏夹和设为首页

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

  4. 采用PHP函数uniqid生成一个唯一的ID

    http://www.daimajiayuan.com/sitejs-17815-1.html

  5. JAVA 新闻

    Oracle已对Java失去兴趣?Java社区能否扭转乾坤? http://news.cnblogs.com/n/549566/ http://mp.weixin.qq.com/s?__biz=MjM ...

  6. HTTPS-HSTS协议(强制客户端使用HTTPS与服务器创建连接)

    HSTS(HTTP Strict Transport Security)国际互联网工程组织IETE正在推行一种新的Web安全协议 HSTS的作用是强制客户端(如浏览器)使用HTTPS与服务器创建连接. ...

  7. QQ登入(5)获取空间相册,新建相册,上传图片到空间相册

    ///////////////////////////////////////////////////////////////////// 获取相册列表:必须先授权登入 1.1.  String mA ...

  8. Unity3D研究院编辑器之脚本设置ToolBar

    Unity版本5.3.2 如下图所示,ToolBar就是Unity顶部的那一横条.这里的所有按钮一般情况下都得我们手动的用鼠标去点击.这篇文章我们说说如果自动操作它们 1.自动点击左边四个按钮 (拖动 ...

  9. python命令行添加Tab键自动补全

    1.编写一个tab的自动补全脚本,名为tab.py #!/usr/bin/python # python tab complete import sys import readline import ...

  10. 开启自启动oracle和实例

    第一步在/etc/rc.d/rc.local中添加下列信息#启动oraclesu - oracle -c '/u01/app/oracle/product/11.2.0/db_1/bin/dbstar ...