[linux basic]--线程
/*************************************************************************
> 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]--线程的更多相关文章
- [linux basic 基础]----线程的属性
在信号量和互斥量例子中,我们都是在程序推出之前利用pthread_join对线程进行再次同步:如果想让thread想创建它的线程返回数据我需要这么做:问题:我们有时候既不需要第二个线程向main线程返 ...
- Linux/Unix 线程同步技术之互斥量(1)
众所周知,互斥量(mutex)是同步线程对共享资源访问的技术,用来防止下面这种情况:线程A试图访问某个共享资源时,线程B正在对其进行修改,从而造成资源状态不一致.与之相关的一个术语临界区(critic ...
- Linux获取线程tid线程名
Linux获取线程tid线程名 1 2 3 4 5 6 //thread name char cThreadName[32] = {0}; prctl(PR_GET_NAME, (unsigned l ...
- Linux编程---线程
首先说一下线程的概念.事实上就是运行在进程的上下文环境中的一个运行流.普通进程仅仅有一条运行流,可是线程提供了多种运行的路径并行的局面. 同一时候,线程还分为核心级线程和用户级线程.主要差别在属于核内 ...
- Linux 多线程 - 线程异步与同步机制
Linux 多线程 - 线程异步与同步机制 I. 同步机制 线程间的同步机制主要包括三个: 互斥锁:以排他的方式,防止共享资源被并发访问:互斥锁为二元变量, 状态为0-开锁.1-上锁;开锁必须由上锁的 ...
- Linux 默认线程栈大小 调优
Linux 线程栈介绍 栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清除的变量的存储区.里面的变量通常是局部变量.函数参数等:和堆相比,栈通常很小. Linux 查询线程栈 1.查看默认的 ...
- Linux内核线程创建
本文旨在简单介绍一下Linux内核线程: 先举个例子: 不插U盘,在Linux命令行中输入:ps -el:然后插上U盘,再次输入:ps -el 会发现多出了下面一行(当然还会有其他的,比如scsi相关 ...
- Linux 下线程的理解
2017-04-03 最近深入研究了下Linux线程的问题,发现自己之前一直有些许误解,特记之…… 关于Linux下的线程,各种介绍Linux的书籍都没有深入去解释的,或许真的如书上所述,Linux本 ...
- Linux中线程使用详解
线程与进程为什么有了进程的概念后,还要再引入线程呢?使用多线程到底有哪些好处?什么的系统应该选用多线程?我们首先必须回答这些问题. 使用多线程的理由之一是和进程相比,它是一种非常"节俭&qu ...
随机推荐
- php 函数积累
array_slice()<?php $a=array("red","green","blue","yellow" ...
- 使用isInEditMode解决可视化编辑器无法识别自定义控件的问题
如果在自定义控件的构造函数或者其他绘制相关地方使用系统依赖的代码, 会导致可视化编辑器无法报错并提示:Use View.isInEditMode() in your custom views to s ...
- hdu3308 线段树——区间合并
更新一个点: 求某个区间的最长连续上升序列: 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 #include <cstdio> #in ...
- c++将引用作为函数的参数---6
原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 引用经常被用作函数参数,使得函数中的变量名成为调用程序中的变量别名.这种传递参数 的方法称为按引用传递. ...
- leetcode 38 Count and Say ---java
这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 --> 111221 --> 312211 --> ..... 1个 ...
- Java—面向对象—构造方法及相关思维导图
先构造一个Book类的代码 package org.hanqi.pn0120; public class Book { //构造方法 //1.方法名和类名一样 //2.没有返回值,不需要加void / ...
- UVA-12436 Rip Van Winkle's Code (线段树区间更新)
题目大意:一个数组,四种操作: long long data[250001]; void A( int st, int nd ) { for( int i = st; i <= nd; i++ ...
- 越狱Season 1-Episode 13: End of the Tunnel
Season 1, Episode 13: End of the Tunnel -Fernando: The name is John Abruzzi. 名字是John Abruzzi A b r u ...
- (译) 强化学习 第一部分:Q-Learning 以及相关探索
(译) 强化学习 第一部分:Q-Learning 以及相关探索 Q-Learning review: Q-Learning 的基础要点是:有一个关于环境状态S的表达式,这些状态中可能的动作 a,然后你 ...
- Thinking Clearly about Performance
http://queue.acm.org/detail.cfm?id=1854041 The July/August issue of acmqueue is out now acmqueue is ...