/***
thread.c
***/
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h> void print_message_function(void* ptr); int main()
{
int tmp1,tmp2;
void* retval;
pthread_t thread1,thread2;
char* message1 = "thread1";
char* message2 = "thread2"; int ret_thrd1,ret_thrd2;
ret_thrd1 = pthread_create(&thread1,NULL,(void*)&print_message_function,(void*)message1);
ret_thrd2 = pthread_create(&thread2,NULL,(void*)&print_message_function,(void*)message2); if(ret_thrd1 != )
{
printf("create thread 1 failed\n");
}
else
{
printf("create thread 1 success\n");
} if(ret_thrd2 != )
{
printf("create thread 2 failed\n");
}
else
{
printf("create thread 2 success\n");
} tmp1 = pthread_join(thread1,&retval);
printf("thread1 return value(retval) is %d\n",(int)retval);
printf("thread1 return value(tmp) is %d\n",tmp1);
if(tmp1 != )
{
printf("cannot join with thread1\n");
}
printf("thread1 end\n"); tmp2 = pthread_join(thread2,&retval);
printf("thread2 return value(retval) is %d\n",(int)retval);
printf("thread2 return value(tmp) is %d\n",tmp2);
if(tmp2 != )
{
printf("cannot join with thread2\n");
}
printf("thread2 end\n");
return ;
} void print_message_function(void* ptr)
{
int i;
for(i = ; i < ; i++)
{
printf("%s:%d\n",(char*)ptr,i);
}
}

运行结果:

exbot@ubuntu:~/wangqinghe/thread/thread_0530$ ./thread
create thread 1 success
create thread 2 success
thread2:0
thread2:1
thread2:2
thread2:3
thread2:4
thread1:0
thread1:1
thread1:2
thread1:3
thread1:4
thread1 return value(retval) is 10
thread1 return value(tmp) is 0
thread1 end
thread2 return value(retval) is 10
thread2 return value(tmp) is 0
thread2 end

/***
simple example
***/
#include<stdio.h>
#include<pthread.h> void thread(void)
{
int i;
for(i = ; i < ; i++)
{
printf("This is a pthread.\n");
}
} int main()
{
pthread_t id;
int i,ret;
ret = pthread_create(&id,NULL,(void*)thread,NULL);
if(ret != )
{
printf("Create pthread error!\n");
exit();
}
for(i = ; i < ; i++)
{
printf("This is the main process.\n");
}
pthread_join(id,NULL);
return ;
}

运行结果:

exbot@ubuntu:~/wangqinghe/thread/thread_0530$ ./thread1
This is the main process.
This is the main process.
This is the main process.
This is a pthread.
This is a pthread.
This is a pthread.

/***
pthread.c
***/
//gcc pthread.c -o pthread -lpthread
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h> void * print_a(void*);
void * print_b(void*); int main()
{
pthread_t t0;
pthread_t t1; if(pthread_create(&t0,NULL,print_a,NULL) == -)
{
puts("fail to create pthread t0");
exit();
}
if(pthread_create(&t1,NULL,print_b,NULL) == -)
{
puts("fail to create pthread t1");
exit();
}
void* result;
if(pthread_join(t0,&result) == -)
{
puts("fail to recollect t0");
exit();
}
if(pthread_join(t1,&result) == -)
{
puts("fail to create recollect t1");
exit();
}
return ;
} void * print_a(void*)
{
for(int i = ; i < ; i++)
{
sleep();
puts("aa");
}
return NULL;
}
void * print_b(void*)
{
for(int i = ; i < ; i++)
{
sleep();
puts("bb");
}
return NULL;
}

运行结果:

exbot@ubuntu:~/wangqinghe/thread$ ./pthread
bb
aa
bb
aa
bb
aa
bb
aa
aa
bb
bb
aa
bb
aa
aa
bb
bb
aa
aa
bb
bb
bb
bb
bb
bb
^C

/***
no_mutex.c
***/
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h> int sharedi = ;
void increase_num(void); int main()
{
int ret;
pthread_t thrd1,thrd2,thrd3;
ret = pthread_create(&thrd1,NULL,(void *)increase_num,NULL);
ret = pthread_create(&thrd2,NULL,(void*)increase_num,NULL);
ret = pthread_create(&thrd3,NULL,(void*)increase_num,NULL);
pthread_join(thrd1,NULL);
pthread_join(thrd2,NULL);
pthread_join(thrd3,NULL); printf("sharedi = %d\n",sharedi); return ;
} void increase_num(void)
{
long i,tmp;
for(i = ; i < ; i++)
{
tmp = sharedi;
tmp = tmp + ;
sharedi = tmp;
}
}

运行结果:

exbot@ubuntu:~/wangqinghe/thread/thread_0611$ ./no_mutex
sharedi = 3000

Thread(简单使用)的更多相关文章

  1. 【转】【C#】【Thread】【Task】多线程

    多线程 多线程在4.0中被简化了很多,仅仅只需要用到System.Threading.Tasks.::.Task类,下面就来详细介绍下Task类的使用. 一.简单使用 开启一个线程,执行循环方法,返回 ...

  2. C++使用thread类多线程编程

    转自:C++使用thread类多线程编程 C++11中引入了一个用于多线程操作的thread类,下面进行简单演示如何使用,以及如果进行多线程同步. thread简单示例 #include <io ...

  3. java多线程(简单介绍)

    简单介绍 线程是程序运行的基本执行单元.当操作系统(不包括单线程的操作系统,如微软早期的DOS)在执行一个程序时,会在系统中建立一个进程,而在这个进程中,必须至少建立一个线程(这个线程被称为主线程)来 ...

  4. Pintos-斯坦福大学操作系统Project详解-Project1

    转载请注明出处. 前言:  本实验来自斯坦福大学cs140课程,只限于教学用途,以下是他们对于Pintos系统的介绍:  Pintos is a simple operating system fra ...

  5. python运维开发(十)----IO多路复用线程基本使用

    内容目录: python作用域 python2.7和python3.5的多继承区别 IO多路复用 socketserver模块源分析 多线程.进程.协程 python作用域  python中无块级作用 ...

  6. c#多线程随记回顾

    C#多线程随记回顾 1.创建多线程方式知道的有三种: ---手动创建Thread.使用线程池.使用task任务 ---手动创建Thread,分两种带参数和不带参数的帮助委托器 eg:  //帮助器委托 ...

  7. Java线程和线程池

    Android中创建线程的方式有,new Thread,new Thread(Runnable),new Thread(Callable)的形式. A. 直接new Thread简单方便. B. ne ...

  8. AbstractQueuedSynchronizer源码分析

    AbstractQueuedSynchronizer源码分析 前提 AQS(java.util.concurrent.locks.AbstractQueuedSynchronizer)是并发编程大师D ...

  9. 06 ASP.net

    ASP.net 第一天 理解浏览器与服务器概念,与WinForm的区别. C# IIS(Internet Information Service) 互联网信息服务 Java(Tomcat) Php(A ...

  10. C#多线程与异步

    1.什么是异步同步 如果一个方法被调用,调用者需要等待该方法被执行完毕之后才能继续执行,则是同步. 如果方法被调用后立刻返回,即使该方法是一个耗时操作,也能立刻返回到调用者,调用者不需要等待该方法,则 ...

随机推荐

  1. Spring实战(七)Bean 的作用域

    1.Spring中bean 的多种作用域 单例(Singleton):整个应用中只创建一个bean 的实例,Spring默认创建单例的bean: 原型(Prototype):每次注入or通过Sprin ...

  2. 怎样理解NodeList的动态集合与静态集合

    NodeList 有两种, 一种是动态集合, 一种是静态集合, 所谓动态集合, 主要是 Node.prototype.childNodes; 返回的子节点集合对文档的节点增删改会即时改变; 而静态集合 ...

  3. Centos7 系统启动docker报错 inotify add watch failed

    环境说明: 最近新装的系统启动docker报错,之前没有遇到过.(之前都是系统直接启动,新装机器无报错的情况) 当时排查了很久没找到问题在哪,观察报错信息如下: 提示表文件失败,没有这个文件或者目录. ...

  4. 3-Perl 基础语法

    Perl 基础语法Perl借用了C.sed.awk.shell脚本以及很多其他编程语言的特性,语法与这些语言有些类似,也有自己的特点.Perl 程序有声明与语句组成,程序自上而下执行,包含了循环,条件 ...

  5. java——ArrayList中remove()方法疑问总结

    其实remove方法和contains方法大同小异,它的原理和contains方法相同https://www.cnblogs.com/lyxcode/p/9453213.html在这篇博客里面有详细说 ...

  6. c语言中gets()的详细用法

    gets从标准输入设备读字符串函数.可以无限读取,不会判断上限,以回车结束读取,所以程序员应该确保buffer的空间足够大,以便在执行读操作时不发生溢出. 从stdin流中读取字符串,直至接受到换行符 ...

  7. Linux下离线安装Docker最新版本

    一.基础环境1.操作系统:CentOS 7.32.Docker版本:18.06.1 官方下载地址(打不开可能需要梯子)3.百度云Docker 18.06.1地址:https://pan.baidu.c ...

  8. sql当前时间往后半年

    select  DATEADD(MONTH, -6, GETDATE()) select  DATEADD(hh, -6, GETDATE())

  9. vue2.0关于for循环 index的使用方法

    <!DOCTYPE html> <html> <head> <title>for循环</title> </head> <b ...

  10. Linux--目录属性

    目录的读属性:表示具有读取目录结构清单的权限.使用ls命令可以将该目录中的文件和子目录的内容列出来. 目录的写属性:表示具有更改目录结构清单的权限.包括以下操作: 建立新的文件与目录 删除已经存在的文 ...