pthread的pthread_mutex_lock 的使用

参考http://haoningabc.iteye.com/blog/1709157
在c中多线程,打印数据互相不影响的例子
关键点在pthread_mutex_lock
无锁的情况
testptread.c

#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include "pthread.h"

void * process(void * arg)
{
int i;
fprintf(stderr, "Starting process %s\n", (char *) arg);
for (i = 0; i < 100; i++) {
write(1, (char *) arg, 1);
// fprintf(stdout, (char *) arg, 1);
}
return NULL;
}
int hello(){
printf("hello");
return 1;
}
int main()
{
int retcode;
pthread_t th_a, th_b;
void * retval;

retcode = pthread_create(&th_a, NULL, process, "a");
if (retcode != 0) fprintf(stderr, "create a failed %d\n", retcode);

retcode = pthread_create(&th_b, NULL, process, "b");
if (retcode != 0) fprintf(stderr, "create b failed %d\n", retcode);

retcode = pthread_join(th_a, &retval);
if (retcode != 0) fprintf(stderr, "join a failed %d\n", retcode);

retcode = pthread_join(th_b, &retval);
if (retcode != 0) fprintf(stderr, "join b failed %d\n", retcode);

return 0;
}

编译:

#!/bin/sh
gcc testptread.c -lpthread

输出结果
[img]http://dl2.iteye.com/upload/attachment/0094/2116/faafbc57-3836-3d2b-b32c-f03758ecda5e.jpg[/img]
a和b的打印每次输出都是不同的乱序的
-------------------------------
带锁的thread
testptread_lock.c

#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include "pthread.h"
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
void * process(void * arg)
{
int i;
fprintf(stderr, "Starting process %s\n", (char *) arg);
pthread_mutex_lock(&mymutex);
for (i = 0; i < 100; i++) {
write(1, (char *) arg, 1);
// fprintf(stdout, (char *) arg, 1);
}
pthread_mutex_unlock(&mymutex);
return NULL;
}
int hello(){
printf("hello");
return 1;
}
int main()
{
int retcode;
pthread_t th_a, th_b;
void * retval;

retcode = pthread_create(&th_a, NULL, process, "a");
if (retcode != 0) fprintf(stderr, "create a failed %d\n", retcode);

retcode = pthread_create(&th_b, NULL, process, "b");
if (retcode != 0) fprintf(stderr, "create b failed %d\n", retcode);

retcode = pthread_join(th_a, &retval);
if (retcode != 0) fprintf(stderr, "join a failed %d\n", retcode);

retcode = pthread_join(th_b, &retval);
if (retcode != 0) fprintf(stderr, "join b failed %d\n", retcode);

return 0;
}

[img]http://dl2.iteye.com/upload/attachment/0094/2118/da58dee3-75ad-37b1-ab6d-bcc71f53a573.jpg[/img]

无论a,b是什么时候开始,一定是一个结束之后,锁释放后, 第二个才开始
------------------------------
如果上面的访问用进程实现
则用如下
[img]http://dl2.iteye.com/upload/attachment/0094/2130/b18ecadd-446a-3c15-9064-aac7c7e5a38b.jpg[/img]


#include <stdio.h>
char buf[]={"check lock!\n"};
int main(int argc,char *argv[]){
int i,p1,p2,fd;
fd=creat("lock.dat",0644);
write(fd,buf,20);
while((p1=fork())==-1);
if(p1==0){
lockf(fd,1,0);
for(i=1;i<=3;i++){
printf("child1!\n");
}
lockf(fd,0,0);
}else{
while((p2=fork())==-1);
if(p2==0){
lockf(fd,1,0);
for(i=1;i<=4;i++){
printf("child2!\n");
}
lockf(fd,0,0);
}else{
printf("parrent!\n");
}
}
close(fd);
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值