threads 1 利用变量同步
来源:互联网 发布:淘宝达人头像怎么更换 编辑:程序博客网 时间:2024/05/17 01:21
[root@localhost ch12]# cat test1.c #include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <pthread.h>char message[]="hello";void* thread_function(void *arg);void *thread_result;int main(){int res;pthread_t a_thread;res=pthread_create(&a_thread,NULL,&thread_function,(void*)message);//if(res!=0){perror("pthread_create error");exit(EXIT_FAILURE);}printf("already create a thread\n");int i=0;while(i++<10){printf("main ");usleep(1000*100);//100ms}}void* thread_function(void *arg){int ii=0;while(ii++<10){printf("sub ");usleep(1000*100);}}[root@localhost ch12]# make test1cc -D_REENTRANT -lpthread test1.c -o test1
[root@localhost ch12]# ./test1already create a thread
main sub main sub main sub main sub sub main sub main sub main main sub sub main main sub [root@localhost ch12]#当执行test1时,先输出了already create a thread然后过了一段时间
所有的main sub main sub .....一下子都输出了,..有点奇怪哦
修改line26 ,加上\n,如下
printf("main \n");便正常,main sub 一个接一个按秩序打印出来
[root@localhost ch12]# make test1cc -D_REENTRANT -lpthread test1.c -o test1[root@localhost ch12]# ./test1already create a threadmain sub sub main sub main main sub sub main sub main main sub sub main main sub main sub [root@localhost ch12]# ./test1already create a threadmain sub sub main sub main main sub main sub main sub main sub sub main sub main sub main [root@localhost ch12]# ./test1already create a threadmain sub sub main sub main main sub main sub main sub main sub main sub main sub main sub [root@localhost ch12]# ./test1already create a threadmain sub sub main main sub main sub main sub main sub main sub main sub sub main sub main [root@localhost ch12]#从./test1的几次输出,也可以看出两个线程是"交替"执行的,但不是完全的交替,或许在竞争cpu时间片
上面那个奇怪的问题并不是出现在线程身上,而是print和sleep的问题,
如下普通例子
[root@localhost ch12]# cat test3.c #include <stdio.h>int main() {printf("hello1");sleep(2);printf("hello2\n");printf("hello3");sleep(2);}[root@localhost ch12]# gcc -o test3 test3.c
[root@localhost ch12]# ./test32 seconds passed then
hello1hello22 seconds passed again then
hello3[root@localhost ch12]#看来printf是遇到\n才会真正输出(到stdout),他的这个特性遇到sleep的时候便显现出来
并且从上面的输出可以看出是两个线程在竞争cpu时间片
因此输出时序不太确定
如果要两个线程有序输出的话,则需要互相勾结好--
--即商量好你输出一次,我再输出,然后你再输出..美其名可曰线程同步
比如下
[root@localhost ch12]# cat test1.c#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <pthread.h>char message[]="hello";void* thread_function(void *arg);void *thread_result;int run_now=1;int main(){int res;pthread_t a_thread;res=pthread_create(&a_thread,NULL,&thread_function,(void*)message);//if(res!=0){perror("pthread_create error");exit(EXIT_FAILURE);}printf("already create a thread\n");int i=0;while(i++<10){ if (run_now == 1){printf("main \n"); run_now = 2;}else{ usleep(1000*100);}}}void* thread_function(void *arg){int ii=0;while(ii++<10){ if (run_now == 2){printf("sub "); run_now = 1;}else{ usleep(1000*100);}}}执行之,有序了,每个线程输出5次
[root@localhost ch12]# make test1cc -D_REENTRANT -lpthread test1.c -o test1[root@localhost ch12]# ./test1already create a threadmain sub main sub main sub main sub main sub [root@localhost ch12]# ./test1already create a threadmain sub main sub main sub main sub main sub [root@localhost ch12]#但线程同步的正当途径应该是信号量和互斥量.
- threads 1 利用变量同步
- 利用条件变量实现线程间同步
- 利用条件变量实现线程间同步
- threads 2-局部变量和全局变量
- Threads
- Threads
- Threads
- Threads
- 线程同步-条件变量
- 线程同步条件变量
- 多线程同步条件变量
- linux同步-条件变量
- Synchronized块同步变量
- linux 条件变量--同步
- 多线程同步条件变量
- 同步和条件变量
- 线程同步---条件变量
- 多线程同步条件变量
- 使用hibeinateTemplate进行批量删除的两种方法
- 线段树模板(插入,搜索)
- android云端网游开发设想
- 据说看完这21个故事的人,30岁前都成了亿万富翁。你是下一个吗?
- 通过开机BIOS响铃来判断计算机故障
- threads 1 利用变量同步
- 按照listview 的列数来导入数据
- struts2 .hibernate一起用出现的问题
- 判断输入的字符是否为数字
- TabHost clear tab后,要新加tab时,使用注意,不然会杯具
- CMMI5
- Flex4 动态添加删除组件
- 当端口被占用时
- 国际域名注册商排名