多线程计数,每个线程累加10个数。

  实现:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h> pthread_mutex_t num_lock; int num = ;
int sum = ; void* foo1()
{
#if 1
pthread_mutex_lock(&num_lock);
for(int i = ;i<;++i)
{
num += ;
sum += num;
printf("thread 1,num = %d,sum = %d\n",num,sum);
usleep();
}
pthread_mutex_unlock(&num_lock);
#else
for(int i = ;i<;++i)
{
num += ;
sum += num;
printf("thread 1,num = %d,sum = %d\n",num,sum);
usleep();
}
#endif
return NULL;
} void* foo2()
{
#if 1
pthread_mutex_lock(&num_lock);
for(int i = ;i<;++i)
{
num += ;
sum += num;
printf("thread 2,num = %d,sum = %d\n",num,sum);
usleep();
}
pthread_mutex_unlock(&num_lock);
#else
for(int i = ;i<;++i)
{
num += ;
sum += num;
printf("thread 2,num = %d,sum = %d\n",num,sum);
usleep();
}
#endif
return NULL;
} int main(int argc,char* argv[])
{
pthread_t tid1,tid2;
int err; pthread_mutex_init(&num_lock,NULL); err = pthread_create(&tid1,NULL,foo1,NULL);
if(err != )
{
printf("error code:%d,reason:%s\n",err,strerror(err));
return -;
} err = pthread_create(&tid2,NULL,foo2,NULL);
if(err != )
{
printf("error code:%d,reason:%s\n",err,strerror(err));
return -;
} //for(int i = 0;i<10;++i)
// printf("main thread, num = %d\n",num++); err = pthread_join(tid1,NULL);
if(err != )
{
printf("error code:%d,reason:%s\n",err,strerror(err));
return -;
}
err = pthread_join(tid2,NULL);
if(err != )
{
printf("error code:%d,reason:%s\n",err,strerror(err));
return -;
} pthread_mutex_destroy(&num_lock); return ;
}

码海拾遗:Linux多线程mutex锁的更多相关文章

  1. 码海拾遗:Linux常用命令(一)

    一.Linux系统安装 系统安装可以分两类:实体机安装Linux,虚拟机(常用虚拟机软件有两种:VMware和VirtualBox)安装Linux. 安装过程网上有很多教程,这里就不赘述了. 二.常用 ...

  2. 码海拾遗:基于MySQL Connector/C++的MySQL操作(连接池)

    1.MySQL安装及简单设置 (1)安装:在OSX系统下,可以使用万能的“brew install”命令来进行安装:brew isntall mysql(默认安装最新版的MySQL) (2)启动:br ...

  3. 笔记1 linux 多线程 互斥锁

    //mutex lock #include<stdio.h> #include<unistd.h> #include<pthread.h> struct test ...

  4. 码海拾遗:strcpy()、strncpy()和strcpy_s()区别

    1.strcpy() 原型:char *strcpy(char *dst,const char *src) 功能:将以src为首地址的字符串复制到以dst为首地址的字符串,包括'\0'结束符,返回ds ...

  5. 码海拾遗:简单Socket(TCP)类实现

    最近刚开始啃Unix网络编程(卷1:套接字联网API),为加深TCP连接的建立和终止的理解与记忆,记下本文,方便以后翻看. 同时留下的还有简单的Socket(TCP)类: mySocket.h #pr ...

  6. 码海拾遗:简述C++(一)

    C++是Bjarne Stroustrup博士于1982年,在C语言的基础上引入并扩充了面向对象的概念后发明的一种新的程序语言.就与C语言的渊源而言,C++可以说是C语言的超集,它兼容C的一切(可能是 ...

  7. 码海拾遗:strstr()、strcmp()和strcpy()实现

    1.strstr()实现 原型:char * strstr(const char * str1, const char * str2) 说明:判断str2是否为str1的子串,如果是则返回str2第一 ...

  8. Linux多线程的使用一:互斥锁

    多线程经常会在Linux的开发中用到,我想把平时的使用和思考记录下来,一是给自己做个备忘,二是分享给可能会用到的人. POSIX标准下互斥锁是pthread_mutex_t,与之相关的函数有: 1 i ...

  9. Linux内核互斥锁--mutex

    一.定义: /linux/include/linux/mutex.h   二.作用及访问规则: 互斥锁主要用于实现内核中的互斥访问功能.内核互斥锁是在原子 API 之上实现的,但这对于内核用户是不可见 ...

随机推荐

  1. Dynamics CRM - Plug-in Class 和 Workflow Class 的用法与区别

    在 Dynamics CRM 开发中,我们可以使用 JavaScript 在前端对 Entity Form 进行数据操作,与此同时,我们也可以使用 C# 写后台插件,其中就包括了 Plug-in Cl ...

  2. postman测试

    Postman接口性能测试 1.从文件中获取参数,然后点击Runner 2.勾选测试用例,配置用例次数.参数文件.返回data等 3.点击run 测试用例 4.查看测试结果 5.测试接口:https: ...

  3. org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'socialCode' in 'class java.lang.String'

    异常: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.Refl ...

  4. bzoj5104 Fib数列(BSGS+二次剩余)

    快AFO了才第一次写二次剩余的题…… 显然应该将Fn写成通项公式(具体是什么写起来不方便而且大家也都知道),设t=((1+√5)/2)n,T=√5N,然后可以得到t-(-1)t/t=√5N,两边同时乘 ...

  5. ant design for vue 上传文件

    1.使用customRequest customRequest 通过覆盖默认的上传行为,可以自定义自己的上传实现 Function 定义customRequest,之前定义action行为会被覆盖,可 ...

  6. CodeForces 438D The Child and Sequence (线段树 暴力)

    传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...

  7. LoadRunner 工具使用

    LoaderRunner 第一天 1.1 性能测试基础 ​ 服务器端性能测试 1.1 什么是性能测试的本质 基于协议模拟用户发出请求(业务的模拟), 对服务器形成一定的负载,来测试服务器的性能指标是否 ...

  8. \b 是单词边界锚点 word-boundary anchor,一个“\b”匹配一个单词的一端,两个“\b”匹配一个单词的头尾两端

    123 $_ = "beforematcha? fter";    124 if(/\b\w+a\b/){    125     print "matched: < ...

  9. mysql之结果集去重

    mysql操作中,经常会遇到对结果集的去重 本篇文章列出几种应对办法: 1.使用distinct做去重,测试了一下,DISTINCT可以支持多列去重 select DISTINCT user_id_t ...

  10. asp.net 获取日期

    //获取日期+时间 DateTime.Now.ToString(); // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToString(); // 20 ...