1. /***
    thread.c
    ***/
    #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<pthread.h>
  4.  
  5. void print_message_function(void* ptr);
  6.  
  7. int main()
  8. {
  9. int tmp1,tmp2;
  10. void* retval;
  11. pthread_t thread1,thread2;
  12. char* message1 = "thread1";
  13. char* message2 = "thread2";
  14.  
  15. int ret_thrd1,ret_thrd2;
  16. ret_thrd1 = pthread_create(&thread1,NULL,(void*)&print_message_function,(void*)message1);
  17. ret_thrd2 = pthread_create(&thread2,NULL,(void*)&print_message_function,(void*)message2);
  18.  
  19. if(ret_thrd1 != )
  20. {
  21. printf("create thread 1 failed\n");
  22. }
  23. else
  24. {
  25. printf("create thread 1 success\n");
  26. }
  27.  
  28. if(ret_thrd2 != )
  29. {
  30. printf("create thread 2 failed\n");
  31. }
  32. else
  33. {
  34. printf("create thread 2 success\n");
  35. }
  36.  
  37. tmp1 = pthread_join(thread1,&retval);
  38. printf("thread1 return value(retval) is %d\n",(int)retval);
  39. printf("thread1 return value(tmp) is %d\n",tmp1);
  40. if(tmp1 != )
  41. {
  42. printf("cannot join with thread1\n");
  43. }
  44. printf("thread1 end\n");
  45.  
  46. tmp2 = pthread_join(thread2,&retval);
  47. printf("thread2 return value(retval) is %d\n",(int)retval);
  48. printf("thread2 return value(tmp) is %d\n",tmp2);
  49. if(tmp2 != )
  50. {
  51. printf("cannot join with thread2\n");
  52. }
  53. printf("thread2 end\n");
  54. return ;
  55. }
  56.  
  57. void print_message_function(void* ptr)
  58. {
  59. int i;
  60. for(i = ; i < ; i++)
  61. {
  62. printf("%s:%d\n",(char*)ptr,i);
  63. }
  64. }

运行结果:

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

  1. /***
  2. simple example
  3. ***/
  4. #include<stdio.h>
  5. #include<pthread.h>
  6.  
  7. void thread(void)
  8. {
  9. int i;
  10. for(i = ; i < ; i++)
  11. {
  12. printf("This is a pthread.\n");
  13. }
  14. }
  15.  
  16. int main()
  17. {
  18. pthread_t id;
  19. int i,ret;
  20. ret = pthread_create(&id,NULL,(void*)thread,NULL);
  21. if(ret != )
  22. {
  23. printf("Create pthread error!\n");
  24. exit();
  25. }
  26. for(i = ; i < ; i++)
  27. {
  28. printf("This is the main process.\n");
  29. }
  30. pthread_join(id,NULL);
  31. return ;
  32. }

运行结果:

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.

  1. /***
  2. pthread.c
  3. ***/
  4. //gcc pthread.c -o pthread -lpthread
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8. #include<unistd.h>
  9. #include<pthread.h>
  10.  
  11. void * print_a(void*);
  12. void * print_b(void*);
  13.  
  14. int main()
  15. {
  16. pthread_t t0;
  17. pthread_t t1;
  18.  
  19. if(pthread_create(&t0,NULL,print_a,NULL) == -)
  20. {
  21. puts("fail to create pthread t0");
  22. exit();
  23. }
  24. if(pthread_create(&t1,NULL,print_b,NULL) == -)
  25. {
  26. puts("fail to create pthread t1");
  27. exit();
  28. }
  29. void* result;
  30. if(pthread_join(t0,&result) == -)
  31. {
  32. puts("fail to recollect t0");
  33. exit();
  34. }
  35. if(pthread_join(t1,&result) == -)
  36. {
  37. puts("fail to create recollect t1");
  38. exit();
  39. }
  40. return ;
  41. }
  42.  
  43. void * print_a(void*)
  44. {
  45. for(int i = ; i < ; i++)
  46. {
  47. sleep();
  48. puts("aa");
  49. }
  50. return NULL;
  51. }
  52. void * print_b(void*)
  53. {
  54. for(int i = ; i < ; i++)
  55. {
  56. sleep();
  57. puts("bb");
  58. }
  59. return NULL;
  60. }

运行结果:

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

  1. /***
  2. no_mutex.c
  3. ***/
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<pthread.h>
  7.  
  8. int sharedi = ;
  9. void increase_num(void);
  10.  
  11. int main()
  12. {
  13. int ret;
  14. pthread_t thrd1,thrd2,thrd3;
  15. ret = pthread_create(&thrd1,NULL,(void *)increase_num,NULL);
  16. ret = pthread_create(&thrd2,NULL,(void*)increase_num,NULL);
  17. ret = pthread_create(&thrd3,NULL,(void*)increase_num,NULL);
  18. pthread_join(thrd1,NULL);
  19. pthread_join(thrd2,NULL);
  20. pthread_join(thrd3,NULL);
  21.  
  22. printf("sharedi = %d\n",sharedi);
  23.  
  24. return ;
  25. }
  26.  
  27. void increase_num(void)
  28. {
  29. long i,tmp;
  30. for(i = ; i < ; i++)
  31. {
  32. tmp = sharedi;
  33. tmp = tmp + ;
  34. sharedi = tmp;
  35. }
  36. }

运行结果:

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. 牛客 132C 简单瞎搞题 (bitset)

    大意: 给定序列$a$的第$i$个元素的取值范围$[L_i,R_i]$, 求$a$的平方和的种类数. 用bitset优化, 复杂度$O(\frac{n^5}{\omega})$ #include &l ...

  2. Struts2 流程原理

    一.流程图 (转) 二.流程详解 1.服务器传递来的请求,通过ActionContextClearUp.other filters.最后到达StrutsPrepareAndExecuteFilter ...

  3. bash 中的 :=、=、:-、-、=?、?、:+、+

    bash 中的 :=.=.:-.-.=?.?.:+.+ 来源 https://www.cnblogs.com/fhefh/archive/2011/04/22/2024750.html 变量替换和变量 ...

  4. mysql if else count 计数

    select mobile,avg(total),sum(click_day*click_money),sum(click_day),count(push_status),sum(clicks),co ...

  5. 和 Python 2.x 说再见!项目移到python3

    如果你仍在使用 2.x,那么是时候将你的代码移植到 Python 3 了. 在技术的长河中,软件.工具.系统等版本的迭代本是常事,但由于使用习惯.版本的兼容性.易用性等因素,很多用户及开发者在使用或做 ...

  6. logback替换log4j

    1.新建logback.xml放在src目录下(放在src下会自动加载,不需要再web.xml配置) 2.引入必要的jar包:

  7. JS-逻辑运算符的与,或,非

    JS-逻辑运算符的与,或,非 1.非 所谓非,就是取反,非真即假,非假即真. 非运算符不仅仅只能用于布尔值,其他数据类型也是可以的,如下: 1.如果操作数是一个对象,返回false 2.如果操作数是一 ...

  8. 分布式缓存Redis+Memcached经典面试题和答案

    Redis相比memcached有哪些优势? (1) memcached所有的值均是简单的字符串,redis作为其替代者,支持更为丰富的数据类型 (2) redis的速度比memcached快很多 ( ...

  9. 第十章、random模块

    目录 第十章.random模块 第十章.random模块 #随机生成0-1之间的小数 import random print(random.random()) print(random.randint ...

  10. mysql 设置服务器的MySQL允许远程访问/外网访问

    设置服务器的MySQL允许远程访问/外网访问 https://blog.csdn.net/weixin_34232363/article/details/85889037