设置线程锁属性,用mutex实现两个进程对同一数据进行处理

博文记录:

有关fork()函数的返回值以及子进程和父进程问题,详见:
https://www.cnblogs.com/yuanshuang/p/5571127.html

有关锁的属性pthread_mutexattr_t问题,详见:
https://www.cnblogs.com/tianrks/p/10785577.html

进程间通信——互斥锁和条件变量,详见:
https://zhuanlan.zhihu.com/p/106143527

进程间共享内存的使用,详见:
https://blog.csdn.net/CLZHIT/article/details/104859183

Linux下“锁”的事儿——互斥锁、自旋锁、条件变量,详见:
https://www.cnblogs.com/tgycoder/p/5442040.html

Q1:通过设置线程锁属性,用mutex实现两个进程各加2千万,最终实现4千万。

#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/time.h>
#include <strings.h>
#include <syslog.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <sys/uio.h>
#include <sys/sendfile.h>
#define ARGS_CHECK(argc, val) {if(argc!=val) {printf("error args\n"); return -1;}}
#define ERROR_CHECK(ret, retval, funcName) {if(ret == retval) {perror(funcName); return -1;}}
#define THREAD_ERROR_CHECK(ret,funcName) {if(ret!=0) {printf("%s:%s\n",funcName,strerror(ret));return -1;}}
#define CHILD_THREAD_ERROR_CHECK(ret,funcName) {if(ret!=0) {printf("%s:%s\n",funcName,strerror(ret));return (void*)-1;}}
#endif
#include "func.h"
#include <iostream>

using namespace std;
#define N 20000000

class TestData
{
public:
    inline void InitValue() { val = 0; }
    inline void ProcessFunc() { val++; }
    inline int GetValue() { return val; }
    inline pthread_mutex_t& GetMutexLock() { return mutex; }
private:
    int val;
    pthread_mutex_t mutex;
};

int main()
{
    int shmid = shmget(1000, 1<<20, IPC_CREAT | 0600);
    if(shmid == -1)
    {
        cout << "shmget occur error" << endl;
    }
    TestData* ptr = static_cast<TestData*>(shmat(shmid, nullptr, 0));
    ptr->InitValue();
    pthread_mutexattr_t mattr;
    pthread_mutexattr_init(&mattr);
    pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
    int ret = pthread_mutex_init(&ptr->GetMutexLock(), &mattr);
    if(ret != 0)
    {
        cout << "pthread_mutex_init occur error" << endl;
    }
    struct timeval start, end;
    gettimeofday(&start, nullptr);
    pid_t pid;
    pid = fork();
    if(pid < 0)
    {
        cout << "fork occur error" << endl;
    }
    else if(pid == 0)
    {
        cout << "I am child process, my process id is " << getpid() << endl;
        for(int i = 0; i < N; i++)
        {
            pthread_mutex_lock(&ptr->GetMutexLock());
            ptr->ProcessFunc();
            pthread_mutex_unlock(&ptr->GetMutexLock());
        }

    }
    else
    {
        cout << "I am parent process, my process id is " << getpid() << endl;
        for(int i = 0; i < N; i++)
        {
            pthread_mutex_lock(&ptr->GetMutexLock());
            ptr->ProcessFunc();
            pthread_mutex_unlock(&ptr->GetMutexLock());
        }
        
        wait(nullptr);
        gettimeofday(&end, nullptr);
        cout << "process result = " << ptr->GetValue() << ", use time = " 
             << (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值