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;
        }
    }
  • 相关阅读:
    编码和解码总结
    编码和解码程序例
    IO流字节流输入输出格式
    递归经典题
    ArrayList去重
    集合
    接口文档
    Vue项目零碎知识(全局js,css配置,element-UI,bs使用, img动态配置,js数组)
    djang项目中的疑问及解决办法(ValueError: Invalid model reference 'apps.user.User'. String model references must be of the form 'app_label.ModelName'.)
    Django项目中出现的错误及解决办法(ValueError: Dependency on app with no migrations: customuser)
  • 原文地址:https://www.cnblogs.com/zhangdewang/p/8688222.html
Copyright © 2011-2022 走看看