2017-08-31 28 views
-1

這是我的第一個問題,我真的覺得像一個noob要求它。應用程序應該運行,但CMD輸入也

當我打開一個CMD窗口時,我做了一個應用程序(無所謂)。 在窗口中,您可以讀取我編程的東西,最終,您可以看到「光標」閃爍(程序正在運行)。

例如:

Void main() 
{ 
    Int i,x; 

    While(1) 
    { i++} //program doing staff 

    //Do at same time 
Scanf("%i", &x) 

    If (x==0) 
    {Exit(0)} 

} 

所以,現在我不能使用窗口再在上鍵入一些命令。

但是我想要做的是,輸入類似「停止」或「保持」的東西應該停止或保持程序。

在這方面我聽說過「線程」,但我無法找到一個好的解釋。

有些想法或幫助會很好! 如果已經有關於此的話題,我很抱歉。

回答

-1

請僅小寫Ç來聲明變量和函數 爲結構的typeof,sizeof相似,INT,焦炭,無效的,同時,等....

編輯: 對不起,不明白「我想你需要幫助做的代碼在C」

現在我知道你想多threa ding功能可以同時做兩個或更多的工作,而無需等待其他工作完成。

好了,要做到這一點,你必須

  • 包括pthread.h這意味着POSIX線程

注意:你應該從這個名字注意到這個庫的Linux OS only

,你用gcc編譯器編譯它

  • 聲明變量pthread到例如:tid的main()作爲目前最流行的名字
  • void *()創建功能和類型的東西什麼它然後創建您的線程main()並通過該代碼爲其分配它的功能:

    pthread_create(& tid,NULL,MyThread,NULL);

在pthread_create()參數:

  1. 一個指向我們創建了即將到來的參數,以填補它pthread_t結構。

  2. 指向一個pthread_attr_t參數螺紋。大部分時間你都可以安全地通過NULL

:本pthread_attr_t>「ARG 2」應當被視爲不透明:任何訪問 比通過並行線程功能的其它的對象是不可移植和 產生不確定的結果。

  • 以你爲線程創建的功能,並應符合師妹
    點>>void,這就是爲什麼我們創建無效*()
  • 注意:鍵入唯一沒有()的函數名,你就會知道爲什麼在接下來的變元t

    1. 在這裏您將參數傳遞給您的函數!,如果沒有參數只是通過NULL

    示例代碼

    #include <stdio.h> 
    #include <pthread.h> 
    int i=0,x=0; //Initialize our variables 
    void *MyThread(void *ANYarg) //arguments must be a pointers to point from `pthread_create` with `NULL` if no need 
    { 
        while(1) //background thread 
         { 
          i++; 
          x++; 
         } 
        return NULL; 
    } 
    
    int main() 
    { 
        char *input; 
        pthread_t tid; //Declare a thread 
        pthread_create(&tid, NULL, MyThread, NULL); //Create the thread 
        while(1) //Printing thread , Uncover increamting of variables 
         { 
          scanf("%s",&input); //whenever you input a value 
          printf("i: %d, x: %d\n",i,x);//will print the i,x values now 
         } 
        return 0; 
    } 
    

    注意:你至少應該建立一個指針變量具體到你的線程函數事業第四參數pthread_create()需要至少有一個要通過NULL

    可以等待一個線程來完成,而不是通過代碼行同時做的工作:

    pthread_join(tid, NULL); 
    

    應該返回0成功

    爲例

    #include <stdio.h> 
    #include <stdlib.h> 
    #include <pthread.h> 
    void *MyCoolthread(void *vargp) 
    { 
        printf("Yes..see me printing success after 3 seconds \n"); 
        sleep(3); 
        printf("Success! \n"); 
        return NULL; 
    } 
    
    int main() 
    { 
        pthread_t tid; 
        printf("Hello, are you there? \n"); 
        sleep(1); 
        pthread_create(&tid, NULL, MyCoolthread, NULL); 
        pthread_join(tid, NULL);  
        printf("Yup i see!\n"); 
        return 0; 
    } 
    

    試評pthread_join(tid, NULL);//看個究竟

    ,並終止您的線程是通過代碼行:

    pthread_exit(&tid); 
    

    返回調用者的任何值

    編輯:我現在注意到你使用windows,所以我建議你用Ubuntu的雙啓動,而如果你真的對C有興趣,可以使用C

    否則你可以使用多線程windows.h標題叫做winapi庫,但我不是專家,所以你可以找到一個simple example here,你應該在你的文章中提到,你想通過你可以編輯的窗口改善它

    我盡我所能清楚,希望它有幫助。

    +0

    注:我認爲沒有必要使用'i'或甚至增加它,但它會很有用,如果你把它作爲一個條件:'while(i <10){i ++; stuff ..}'''limit''輸入的數量和退出'while-loop' –

    +0

    好吧,也許我的問題沒有被問及那麼好。 但這不是我要找的。 我想使用多線程,所以我可以同時做兩件事。 –

    +0

    我不知道你的意思是多線程的方式我完全編輯答案^^ –

    相關問題