zoukankan      html  css  js  c++  java
  • pthread linux mutet:example1

    #include<iostream>
    #include<unistd.h>
    #include<pthread.h>
    #include<string>
    using namespace std;
    
    
    #define START 1
    #define END 0
    int status = START;
    void* queuewhile(void *);
    void* dosomething(void *);
    pthread_mutex_t mutex;
    int main()
    {
        //int status = START;
        cout << "please,enter something, if the number is larger.the process ends"<<endl;
        pthread_t thread1, thread2;
        sleep(2);
        pthread_mutex_init(&mutex, NULL);
        string strMsg = "hello,world";
        pthread_create(&thread1, NULL, queuewhile,(void*)strMsg.c_str());
        pthread_create(&thread2, NULL, dosomething, NULL);
        pthread_join(thread1, NULL);
        pthread_mutex_destroy(&mutex);
        cout << "the process is closing"<<endl;
        return 0;
    }
    void* dosomething(void *)
    {
        int a = 16;
        int b = 0;
        while(cin >> b)
        {
            pthread_mutex_lock(&mutex);
            cout<< "something happened"<< endl;
            if(b>a)
            {
                cout<< "now,we shut down!"<<endl;
                status = END;
            }else
            {
                cout<< "ops, we just keep go on!"<<endl;
            }
            pthread_mutex_unlock(&mutex);
        }
    }
    
    
    void* queuewhile(void *msg)
    {
        string strMsg = (char*)msg;
        if(strMsg.empty())
        {
            cout<< "there no thing"<< endl;
        }else
        {
            while(status)
            {
                pthread_mutex_lock(&mutex);
                cout<<strMsg<<endl;
                pthread_mutex_unlock(&mutex);
                sleep(1);
            }
            //cout << strMsg<<endl;
        }
    }

     g++ -o test domything.cpp -lpthread

    #include<iostream>
    #include<unistd.h>
    #include<pthread.h>
    #include<string>
    using namespace std;
    
    
    #define START 1
    #define END 0
    int status = START;
    class MSG{
    public:
       int m_state;
       string m_msg;
    };
    static MSG g_msg;
    void* queuewhile(void *);
    void* dosomething(void *);
    pthread_mutex_t mutex;
    int main()
    {
        //int status = START;
        cout << "please,enter something, if the number is larger.the process ends"<<endl;
        pthread_t thread1, thread2;
        sleep(2);
        pthread_mutex_init(&mutex, NULL);
        string strMsg = "hello,world";
        pthread_create(&thread1, NULL, queuewhile,(void*)strMsg.c_str());
        pthread_create(&thread2, NULL, dosomething, NULL);
        pthread_join(thread1, NULL);
        pthread_mutex_destroy(&mutex);
        cout << "the process is closing"<<endl;
        return 0;
    }
    void* dosomething(void *)
    {
        string a = "q";
        string b = "";
        while(cin >> b)
        {
            pthread_mutex_lock(&mutex);
            cout<< "something happened"<< endl;
            if(a.compare(b) == 0)
            {
                cout<< "now,we shut down!"<<endl;
                status = END;
            }else if(b.compare("w") ==0)
            {
                cin >>b;
                //cout<< "ops, we just keep go on!"<<endl;
                g_msg.m_state += 1;
                g_msg.m_msg = b;
                b.clear();
            }
            pthread_mutex_unlock(&mutex);
        }
    }
    
    
    void* queuewhile(void *msg)
    {
        string strMsg = (char*)msg;
        if(strMsg.empty())
        {
            cout<< "there no thing"<< endl;
        }else
        {
            while(status)
            {
                pthread_mutex_lock(&mutex);
                cout<<strMsg<<endl;
                if(g_msg.m_state > 0)
                {
                    cout <<"the message is:"<< g_msg.m_msg<<endl;
                    g_msg.m_state --;
                }
                pthread_mutex_unlock(&mutex);
                sleep(1);
            }
            //cout << strMsg<<endl;
        }
    }

    //exec child process in the thread

    #include<iostream>
    #include<cstdlib>
    #include<unistd.h>
    #include<pthread.h>
    #include<string>
    using namespace std;
    
    
    #define START 1
    #define END 0
    int status = START;
    class MSG{
    public:
       int m_state;
       string m_msg;
    };
    static MSG g_msg;
    void* queuewhile(void *);
    void* dosomething(void *);
    pthread_mutex_t mutex;
    int main()
    {
        //int status = START;
        cout << "please,enter something, if the number is larger.the process ends"<<endl;
        pthread_t thread1, thread2;
        sleep(2);
        pthread_mutex_init(&mutex, NULL);
        string strMsg = "hello,world";
        pthread_create(&thread1, NULL, queuewhile,(void*)strMsg.c_str());
        pthread_create(&thread2, NULL, dosomething, NULL);
        pthread_join(thread1, NULL);
        pthread_mutex_destroy(&mutex);
        cout << "the process is closing"<<endl;
        return 0;
    }
    void* dosomething(void *)
    {
        string a = "q";
        string b = "";
        while(cin >> b)
        {
            pthread_mutex_lock(&mutex);
            cout<< "something happened"<< endl;
            if(a.compare(b) == 0)
            {
                cout<< "now,we shut down!"<<endl;
                status = END;
            }else if(b.compare("w") ==0)
            {
                cin >>b;
                //cout<< "ops, we just keep go on!"<<endl;
                g_msg.m_state += 1;
                g_msg.m_msg = b;
                b.clear();
            }
            pthread_mutex_unlock(&mutex);
        }
    }
    
    
    void* queuewhile(void *msg)
    {
        string strMsg = (char*)msg;
        if(strMsg.empty())
        {
            cout<< "there no thing"<< endl;
        }else
        {
            while(status)
            {
                pthread_mutex_lock(&mutex);
                cout<<strMsg<<endl;
                if(g_msg.m_state > 0)
                {
                    string strPath = getenv("PWD");
                    //cout << "the path is :" << strPath<< endl;
                    strPath.append("/child ");
                    strPath.append(g_msg.m_msg.c_str());
                    system(strPath.c_str());
                    cout <<"the message is:"<< g_msg.m_msg<<endl;
                    g_msg.m_state --;
                }
                pthread_mutex_unlock(&mutex);
                sleep(1);
            }
            //cout << strMsg<<endl;
        }
    }
  • 相关阅读:
    已知自然数A、B不互质,A、B最大公约数和最小公倍数之和为35,那么A+B的最小值是多少?
    mysql null字段 空字段 查询效率
    sql注入和网站攻击思路
    软件服务新时代:开源软件的盈利模式
    eclipse maven插件配置,jdk全局版本调整
    spring事务(isolation隔离级别,propagation事务传播属性)
    GBDT 算法
    博客园的 “随笔、文章、新闻、日记”有啥区别
    1.3 Java中的标识符和关键字
    1.1 Java 的概述
  • 原文地址:https://www.cnblogs.com/zhangdewang/p/8688222.html
Copyright © 2011-2022 走看看