• 多线程多进程


    多线程:

    TODO:各函数意义,定时器实现,循环的实现,多线程同步

    多线程同步:互斥锁

    定时器、循环实现:setitimer(ITIMER_REAL, &val, NULL)

    http://blog.csdn.net/lixianlin/article/details/25604779

    定时器信号:signal(SIGALRM, time_proc)

    setpriority:设置进程和用户的进程执行优先权。

    int main(int argc, char *argv[])

    {

      union sigval tsval;

      pthread_t recv_thread;

      pthread_t thread;

      int result = 1;

      pthread_mutex_t info_mutex; //互斥锁

           setpriority(PRIO_PROCESS, 0, -20);

          

      pthread_create(&recv_thread, NULL, udp_server, (void *)NULL);

      pthread_mutex_init(&info_mutex, NULL);

           pthread_create(&thread, NULL, proc, (void *)NULL);

           signal(SIGALRM, time_proc);       

      while(1)

       {

             pause(); //挂起,直到收到信号,本例中是SIGALRM

       }

      return 0;

    }

    #include <iostream>

    #include <unordered_map>

    #include <iostream>

    #include <thread>

    #include <mutex>

    #include <unistd.h>

    using namespace std;

    using std::cout;

    using std::endl;

    using std::unordered_map;

    void Fun_1();          

    void Fun_2();         

    std::mutex mtx;                //定义mutex类的对象mtx构造互斥元,互斥占有一个变量,一段时间内仅一个线程可以访问

    unordered_map<unsigned int,string> gMap ;

    int main()

    {

        std::thread thrd_1(Fun_1);     //创建线程thrd_1thrd_1调用函数Fun_1

        std::thread thrd_2(Fun_2);     //创建线程thrd_2thrd_2调用函数Fun_2

        thrd_1.join();                 //join()函数启动子线程而阻塞主线程,子线程会按照开启的先后顺序同步运行,当子线程运行结束后,才会继续运行主线程

        thrd_2.join();                 //启动线程thrd_2,并且阻塞主线程,等到线程thrd_2运行结束后,再继续运行主线程;

       for (auto it = gMap.begin(); it != gMap.end();++it ){

          cout<<"main:"<<it->first<<":"<<it->second.c_str()<<endl;

          sleep(1);

       }

        return 0;

    }

    void Fun_1()

    {

       mtx.lock();

       for (unsigned int i = 0; i < 10; ++i){

          gMap[i] = std::to_string(i);

          cout<<"func1:"<<i<<endl;

          sleep(1);

       }

       mtx.unlock();

    }

    void Fun_2()

    {

       mtx.lock();

       for (unsigned int i = 0; i < 10; ++i){

          gMap[i] = std::to_string(i+1);

          cout<<"func2:"<<i<<endl;

          sleep(1);

       }

       mtx.unlock();

    }

    多进程:

    http://www.cnblogs.com/tgycoder/p/5263644.html

    TODO:进程的简介

    void main(){

        int i;

        if (fork() == 0) {

            /* 子进程程序 */

            for (i = 1; i <1000; i++)

                printf("This is child process ");

        }

        else {

            /* 父进程程序*/

            for (i = 1; i <1000; i++)

                printf("This is parent process ");

        }

    }

    linux环境编程及linux网络编程:

    TODO

    计算机操作系统及计算机体系结构:

  • 相关阅读:
    Kruskal
    克鲁斯卡尔
    克鲁斯卡尔
    实践是检验真理的唯一标准 脱壳篇02
    Kruskal
    克鲁斯卡尔算法讲解
    实践是检验真理的唯一标准 脱壳篇02
    最小生成树(普里姆算法) 数据结构和算法62
    克鲁斯卡尔算法讲解
    最小生成树(普里姆算法) 数据结构和算法62
  • 原文地址:https://www.cnblogs.com/sunnypoem/p/9563162.html
Copyright © 2020-2023  润新知