• linux pthread线程相关,只运行唯一进程相关


    #include <QCoreApplication>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include <unistd.h>
    #include <errno.h>
    #include <pthread.h>
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include<dirent.h>
    using namespace std;
    
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    int count_value = 0;
    
    void * MyFunc(void * arg)
    {
        if(arg == NULL)
        {
            printf("param is not allow NULL.
    ");
            return NULL;
        }
        //加锁--所有线程都能访问的全局变量加锁
        pthread_mutex_lock(&mutex);
        int *pnumx = (int*) arg;
        int i = 0;
        while(i < 1000)
        {
            printf("thread%d count=%d
    ", *pnumx, count_value++);
            sleep(1);
            i++;
        }
        pthread_mutex_unlock(&mutex);
        return NULL;
    }
    
    void * MyFunc2(void * arg)
    {
        if (arg == NULL)
        {
            printf("param is not allow NULL!
    ");
            return NULL;
        }
        //这个锁和myfunc中的锁是同一个锁,myfunc被锁,这里也会被锁
        pthread_mutex_lock(&mutex);
        int * pnumx = (int *) arg;
        int i = 0;
        while (i < 1000)
        {
            printf("thread%d count=%d
    ", *pnumx, count_value++);
            sleep(1);
            i++;
        }
        //解锁
        pthread_mutex_unlock(&mutex);
        return NULL;
    }
    
    //Already running return true.
    bool IsSingleProcess ()
    {
    
        int no_os_flag=1;
    
    #ifdef linux
    
        no_os_flag=0;
    
        std::cout<<"It is in Linux OS!"<<endl;
    
    #endif
    
    #ifdef _UNIX
    
        no_os_flag=0;
    
        std::cout<<"It is in UNIX OS!"<<endl;
    
    #endif
    
    #ifdef __WINDOWS_
    
        no_os_flag=0;
    
        std::cout<<"It is in Windows OS!"<<endl;
    
    #endif
    
    #ifdef _WIN32
    
        no_os_flag=0;
    
        std::cout<<"It is in WIN32 OS!"<<endl;
    
    #endif
    
        if(1==no_os_flag){
    
            std::cout<<"No OS Defined ,I do not know what the os is!"<<endl;
    
        }
    
    
        long pid = 0;
        char full_name[1024] = {0};
        char proc_name[1024] = {0};
        int fd;
        pid = getpid();
        std::cout << "pid = " << pid << " ";
        sprintf(full_name, "/proc/%ld/cmdline", pid);
        if (access(full_name, F_OK) == 0)
        {
            fd = open (full_name, O_RDONLY);
            if (fd == -1)
                return false;
            read (fd, proc_name, 1024);
            close (fd);
        }
        else
            return false;
    
        char self_proc_name[512] = {0};
        char * p = proc_name;
        int pt = 0;
        while (*p != ' ' && *p != '')
        {
            self_proc_name[pt] = *p;
            p++;
            pt++;
        }
        string self_final_name = basename(self_proc_name);
        std::cout << " self_final_name = " << self_final_name << std::endl << flush;
        DIR *dir;
        struct dirent * result;
        dir = opendir ("/proc");
        while ((result = readdir (dir)) != NULL)
        {
            if (! strcmp(result->d_name, ".") || ! strcmp (result->d_name, "..") || ! strcmp (result->d_name, "thread-self")
                    || ! strcmp (result->d_name, "self") || atol (result->d_name) == pid)
                continue;
            memset(full_name, 0, sizeof(full_name));
            memset(proc_name, 0, sizeof(proc_name));
            sprintf(full_name, "/proc/%s/cmdline", result->d_name);
            if (access(full_name, F_OK) == 0)
            {
                fd = open (full_name, O_RDONLY);
                if (fd == -1)
                    continue;
                read (fd, proc_name, 1024);
                close (fd);
                char *q = proc_name;
                pt = 0;
                memset(self_proc_name, 0, sizeof (self_proc_name));
                while (*q != ' ' && *q != '')
                {
                    self_proc_name[pt] = *q;
                    q++;
                    pt++;
                }
                string other_final_name = basename(self_proc_name);
                if (self_final_name == other_final_name)
                {
                    cout << "full_name = " << full_name << endl << flush;
                    cout << "other_final_name: " << other_final_name << endl;
                    return true;
                }
            }
        }
        return false;
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        //condition compile
        #ifdef _WIN32
            if(true == IsSingleProcess())
            {
                std::cout << "process is running in another place" << endl << flush;
                return 0;
            }
        #else
            if(true == IsSingleProcess())  //Already running return true.
            {
                std::cout << "......"<< endl;
                return 0;
            }
        #endif
    
        cout<<".............START............."<<endl;
    
        pthread_t thr1,thr2;
        pthread_attr_t attr;
    
        pthread_attr_init(&attr);
        //设置进程为可分离状态
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        int aa = 1, bb = 2;
    
        //创建线程
        if (pthread_create(&thr1, &attr, MyFunc, &aa) != 0)
        {
            printf("create thread is failed ! error message :%s
    ",
                   strerror(errno));
            return -1;
        }
        //释放进程属性对象
        pthread_attr_destroy(&attr);
    
        if (pthread_create(&thr2, NULL, MyFunc2, &bb) != 0)
        {
            printf("create thread is failed ! error message :%s
    ",
                   strerror(errno));
            return -1;
        }
        pthread_join(thr2, NULL);
        printf("main end
    ");
        return 0;
    
        return a.exec();
    }
  • 相关阅读:
    mysql 执行计划 explain
    深度学习论文翻译解析(二十):YOLOv4: Optimal Speed and Accuracy of Object Detection
    卷积神经网络学习笔记——轻量化网络MobileNet系列(V1,V2,V3)
    OpenCV计算机视觉学习(13)——图像特征点检测(Harris角点检测,sift算法)
    人工智能必备数学基础:概率论与数理统计(2)
    人工智能必备数学基础:概率论与数理统计(1)
    深度学习论文翻译解析(十九):Searching for MobileNetV3
    深度学习论文翻译解析(十八):MobileNetV2: Inverted Residuals and Linear Bottlenecks
    深度学习论文翻译解析(十七):MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
    卷积神经网络学*笔记——SENet
  • 原文地址:https://www.cnblogs.com/mathyk/p/11589642.html
Copyright © 2020-2023  润新知