线程的两种同步方式测试
来源:互联网 发布:Linux netstat -i 编辑:程序博客网 时间:2024/04/23 18:49
创建线程:
#include<pthread.h>#include<stdio.h>#include<unistd.h>void *foo(void *arg){printf("%s", (char*)arg);getchar();pthread_exit("!!!!!!!!!\n");}int main(){pthread_t pt;void *result="aaa";char arg[] = "~~~~~~";pthread_create(&pt, NULL, foo, arg);sleep(5);pthread_join(pt, &result);printf("result:%s\n", (char*)result);return 0;}
信号量同步:
#include<semaphore.h>#include<pthread.h>#include<stdio.h>#include<string.h>sem_t sem;sem_t semw;char buf[20];void *foo(void *t){while(1){sem_wait(&sem);sleep(3);printf("--------------%s\n", buf);sem_post(&semw);}pthread_exit("");}int main(){pthread_t thread;sem_init(&sem, 0, 0);sem_init(&semw, 0, 1);pthread_create(&thread, NULL,foo, "");do{sem_wait(&semw);gets(buf);sem_post(&sem);}while(strcmp(buf,"exit")!= 0);return 0;}
互斥锁同步:
#include<pthread.h>#include<stdio.h>#include<stdlib.h>#define _LOCK_int value1, value2;pthread_mutex_t mutex;unsigned int count = 0;void *foo(void *t){while(1){#ifdef _LOCK_pthread_mutex_lock(&mutex);#endifif(value1 != value2){printf("coutn=%d, value1=%d, value2=%d\n",count, value1,value2);}#ifdef _LOCK_pthread_mutex_unlock(&mutex);#endif}}int main(){pthread_t pth;if(pthread_create(&pth, NULL, foo, NULL)<0){perror("");exit(-1);}while(1){count++;#ifdef _LOCK_pthread_mutex_lock(&mutex);#endifvalue1 = count;value2 = count;#ifdef _LOCK_pthread_mutex_unlock(&mutex);#endif}return 0;}
- 线程的两种同步方式测试
- 线程同步两种方式
- Java中线程同步的两种方式
- 线程-实现线程的两种方式
- 多线程创建的两种方式和线程同步问题实例
- 线程(Thread)同步的两种表现形式
- 线程同步的两种防护四
- 5.7多线程同步的两种方式
- SqlServer2008 数据库同步的两种方式
- JavaLearning:synchronized同步的两种方式
- 多线程同步工作的两种方式
- java实现同步的两种方式
- MongoDB 同步 Hive 的两种方式
- 两种创建线程的方式/线程的生命周期及各自使用同步锁时的区别
- 线程的同步方式
- 线程同步的方式
- 线程同步的方式
- 04.多线程--06.【同步方式在线程两种创建方式中的可行性】【同步代码块和同步函数的关系】【多线程程序设计思路总结】
- HBase介绍
- java中synchronized用法
- 第三章:Creating Applications and activities-(四)Android的应用的生命周期
- gdb core 调试
- 一个简单,但是非常有用的css样式,有木有
- 线程的两种同步方式测试
- PPP-CHAP原理与配置
- HBase命令
- 第三章:Creating Applications and activities-(五)理解应用的优先级以及进程状态
- int转string string转int
- 第三章:Creating Applications and activities-(六)外部资源
- A simple asp
- hibernate 动态生成表
- VC2005的程序文件分发问题及解决