zoukankan      html  css  js  c++  java
  • 2012.02.09(如何在Linux的Qt中,在while中按键退出)

    使用pthread_create()函数创建两个线程,一个线程的while()循环用于刷新屏幕的内容。一个线程用于getch()函数。当getch()函数接受键盘输入后,使用pthread_exit()退出它所在的函数的线程,并使用pthread_cancel()函数将那个有while()循环的函数的线程关闭  //-lpthread

    //例程:

    #include <stdio.h>

    #include <pthread.h>

    #include <string.h>

    #include <sys/types.h>

    #include <unistd.h>

    pthread_t ntid;

    void *thr_fn(void *arg){

     printf("Hello!\n");

    return ((void *)0);

    }

    int err;

    err=pthread_create(&ntid,NULL,thr_fn,NULL);

    /////////////////////////////////////////////////////////////////////////////////////////////

    创建两个线程:

    void main()

    {

      pthread_t  tid1,tid2;

      pthread_create(&tid1,NULL,(void *)juzhen1,NULL);

      pthread_create(&tid2,NULL,(void *)juzhen2,NULL);

    }

    ////////////////////////////////////////////////////////////

    void *thread_A_function(void *arg1);//Linux下的线程1

    void *thread_B_function(void *arg2);//Linux下的线程2

    char message1[]="I am now in the process!"

    int main()

    {

      int RetVal1,RetVal2;//保存子线程a和b的返回值

      pthread_t a_thread;//子线程1

      pthread_t b_thread;//子线程2

      void *threadA_result;

      void *threadB_result;

    //开始创建两个线程

    RetVall=pthread_create(&a_thread,NULL,thread_A_function,(void *)message1);//

    sleep(1);

    RetVall=pthread_create(&b_thread,NULL,thread_B_function,(void *)message1);

    if(RetVal1!=0)

    {

     perror("ThreadA creation failed!");

     exit(EXIT_FAILURE);

    }

    if(RetVal2!=0)

    {

      perror("ThreadB creation failed!");

      exit(EXIT_FAILURE);

    }

    printf("I am now in the process,waiting the thread finish....\n");

    RetVall=pthread_join(a_thread,&threadA_result);//等待线程a结束

    if(RetVal1!=0)

    {

       perror("Thread join failed!");

       exit(EXIT_FAILURE);

    }

    printf(ThreadA joined,the threadA return %s\n",(char *)threadA_result);

    printf(Message is now %s\n",message1);

    RetVal2=pthread_join(b_thread,&threadB_result);//等待线程b结束

    if(RetVal2!=0)

    {

      perror("Thread join failed!");

      exit(EXIT_FAILURE);

    }

    printf(ThreadB joined,the threadB return %s\n",(char *)threadB_result);

    printf("Message is now %s\n",message1);

    exit(EXIT_SUCCESS);

    }

    void *thread_A_function(void *arg1)

    {

       strcpy(message,"I has changed by threadA");

       printf("thread_functionA is running.Argument was %s\n",(char *)arg1);

       sleep(3);

       pthred_exit("THREAD_001);

    }

    void *thread_B_function(void *arg2)

    {

      strcpy(message1,"I has changed by threadB");

      printf("thread_functionB is running.Argument was %s\n",(char *)arg2);

      sleep(4);

      pthread_exit("THREAD_002");

    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Qt按键事件:

    在网址:http://blog.163.com/happyboyyu@yeah/blog/static/107334462201031852722111/

    有详细的说明

  • 相关阅读:
    OC
    OC
    OC
    OC
    OC
    Oracle wm_concat()函数
    字符串拼接
    easyui扩展数据表格点击加号拓展
    子tab里面新增tab(top.jQuery)
    combox datagrid重复请求问题
  • 原文地址:https://www.cnblogs.com/itxiaocaiyidie/p/2343527.html
Copyright © 2011-2022 走看看