linux下,两个线程依次执行
2014-01-14 18:46
375 查看
如何实现Linux下双线程的依次执行?
法1:设置一个flag变量, 线程1执行后,设置flag为2;线程2执行后,设置flag为1;代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
int num = 0;
int flag = 1;
void function1()
{
while(num < 10000)
{
if(flag == 1)
{
num ++;
//printf("thread %d, num = %d\n",syscall(SYS_gettid),num);
flag = 2;
}
}
}
void function2()
{
while(num < 10000)
{
if(flag == 2)
{
num ++;
//printf("thread %d,num = %d\n",syscall(SYS_gettid),num);
flag = 1;
}
}
}
void main()
{
int ret1, ret2;
struct timeval start ,end;
float use;
gettimeofday(&start,NULL);
printf("getpid() = %d\n\n",getpid());
pthread_t p1,p2;
if(ret1 != 0)
{
printf("thread 1 create failed ..\n");
return;
}
ret2 = pthread_create(&p2, NULL, (void *)&function2, NULL);
if(ret2 != 0)
{
printf("thread 2 create failed ..\n");
return;
}
pthread_join(p1, NULL);
pthread_join(p2, NULL);
//pthread_mutex_destroy(&t1);
//pthread_cond_destroy(&cond1);
gettimeofday(&end, NULL);
use = 1000000*(start.tv_sec - end.tv_sec) + start.tv_usec - end.tv_usec ;
use = -use /1000000;
printf("all has done... %f s time elapsed..\n", use);
return;
}
上述代码完全可以胜任,问题在于打印10000次耗费时间20多秒。
肯定存在更好的办法:如下,借用线程互斥量和条件等待:
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
int num = 0;
bool des = true;
pthread_mutex_t t1 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
int flag = 1;
void function1()
{
num ++ ;
pthread_cond_broadcast(&cond1);
while(num < 10000000)
{
pthread_mutex_lock(&t1);
while(flag == 1)
pthread_cond_wait(&cond1, &t1);
num ++ ;
printf("1 : num = %d\n",num);
pthread_mutex_unlock(&t1);
flag = 1;
pthread_cond_broadcast(&cond1);
}
}
void function2()
{
while(num < 10000000)
{
pthread_mutex_lock(&t1);
while(flag ==2)
pthread_cond_wait(&cond1, &t1);
num ++;
printf("2 : num = %d\n",num);
pthread_mutex_unlock(&t1);
flag = 2;
pthread_cond_broadcast(&cond1);
}
}
void main()
{
int ret1, ret2;
struct timeval start ,end;
float use;
gettimeofday(&start,NULL);
printf("getpid() = %d\n\n",getpid());
pthread_t p1,p2;
pthread_mutex_init(&t1, NULL);
pthread_cond_init(cond1, NULL);
ret1 = pthread_create(&p1, NULL, (void *)&function1, NULL);
if(ret1 != 0)
{
printf("thread 1 create failed ..\n");
return;
}
ret2 = pthread_create(&p2, NULL, (void *)&function2, NULL);
if(ret2 != 0)
{
printf("thread 2 create failed ..\n");
return;
}
pthread_join(p1, NULL);
pthread_join(p2, NULL);
pthread_mutex_destroy(&t1);
pthread_cond_destroy(&cond1);
gettimeofday(&end, NULL);
use = 1000000*(start.tv_sec - end.tv_sec) + start.tv_usec - end.tv_usec ;
use = -use /1000000;
printf("all has done... %f s time elapsed..\n", use);
return;
}
效率提升至少3个数量级。
法1:设置一个flag变量, 线程1执行后,设置flag为2;线程2执行后,设置flag为1;代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
int num = 0;
int flag = 1;
void function1()
{
while(num < 10000)
{
if(flag == 1)
{
num ++;
//printf("thread %d, num = %d\n",syscall(SYS_gettid),num);
flag = 2;
}
}
}
void function2()
{
while(num < 10000)
{
if(flag == 2)
{
num ++;
//printf("thread %d,num = %d\n",syscall(SYS_gettid),num);
flag = 1;
}
}
}
void main()
{
int ret1, ret2;
struct timeval start ,end;
float use;
gettimeofday(&start,NULL);
printf("getpid() = %d\n\n",getpid());
pthread_t p1,p2;
if(ret1 != 0)
{
printf("thread 1 create failed ..\n");
return;
}
ret2 = pthread_create(&p2, NULL, (void *)&function2, NULL);
if(ret2 != 0)
{
printf("thread 2 create failed ..\n");
return;
}
pthread_join(p1, NULL);
pthread_join(p2, NULL);
//pthread_mutex_destroy(&t1);
//pthread_cond_destroy(&cond1);
gettimeofday(&end, NULL);
use = 1000000*(start.tv_sec - end.tv_sec) + start.tv_usec - end.tv_usec ;
use = -use /1000000;
printf("all has done... %f s time elapsed..\n", use);
return;
}
上述代码完全可以胜任,问题在于打印10000次耗费时间20多秒。
肯定存在更好的办法:如下,借用线程互斥量和条件等待:
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
int num = 0;
bool des = true;
pthread_mutex_t t1 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
int flag = 1;
void function1()
{
num ++ ;
pthread_cond_broadcast(&cond1);
while(num < 10000000)
{
pthread_mutex_lock(&t1);
while(flag == 1)
pthread_cond_wait(&cond1, &t1);
num ++ ;
printf("1 : num = %d\n",num);
pthread_mutex_unlock(&t1);
flag = 1;
pthread_cond_broadcast(&cond1);
}
}
void function2()
{
while(num < 10000000)
{
pthread_mutex_lock(&t1);
while(flag ==2)
pthread_cond_wait(&cond1, &t1);
num ++;
printf("2 : num = %d\n",num);
pthread_mutex_unlock(&t1);
flag = 2;
pthread_cond_broadcast(&cond1);
}
}
void main()
{
int ret1, ret2;
struct timeval start ,end;
float use;
gettimeofday(&start,NULL);
printf("getpid() = %d\n\n",getpid());
pthread_t p1,p2;
pthread_mutex_init(&t1, NULL);
pthread_cond_init(cond1, NULL);
ret1 = pthread_create(&p1, NULL, (void *)&function1, NULL);
if(ret1 != 0)
{
printf("thread 1 create failed ..\n");
return;
}
ret2 = pthread_create(&p2, NULL, (void *)&function2, NULL);
if(ret2 != 0)
{
printf("thread 2 create failed ..\n");
return;
}
pthread_join(p1, NULL);
pthread_join(p2, NULL);
pthread_mutex_destroy(&t1);
pthread_cond_destroy(&cond1);
gettimeofday(&end, NULL);
use = 1000000*(start.tv_sec - end.tv_sec) + start.tv_usec - end.tv_usec ;
use = -use /1000000;
printf("all has done... %f s time elapsed..\n", use);
return;
}
效率提升至少3个数量级。
相关文章推荐
- 开源项目-Mplayer在windows下的编译总结
- Linux下获取线程TID的方法——gettid()
- Linux下获取线程TID的方法——gettid()
- Linux下获取线程TID的方法——gettid()
- linux下获取系统时间
- rhel 6.3 建立centos yum
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- Linux统计某文件夹下文件、文件夹的个数
- linux 内存管理浅析(二)
- 【原创】Linux下的ngix服务器安装步骤
- linux内存管理浅析(一)
- Linux下如何删除乱码文件
- Linux下的文件比较工具
- 收集linux几个快捷键
- linux驱动开发之自动创建设备节点
- linux 新建用户、用户组 以及为新用户分配权限
- linux sftp and unix ftp
- Linux驱动4_NETFILTER
- linux 定时删除过期文件夹
- Linux恢复数据