2014-12-05 133 views
-3

我是編程新手,所以這聽起來可能聽起來不太好,但在我做了一些研究之後,我明白了我可以創建一個新線程,以便多個代碼塊可以並行運行。有人能解釋我如何創建一個新的線程。我在Windows上使用Visual Studio。如何創建一個新線程

+0

你用'C'編碼而不是'C++'嗎?你使用的是哪個版本的Visual Studio? – druckermanly 2014-12-05 07:16:55

+0

'pthread_create' ?? – 2014-12-05 07:17:28

+0

我正在使用Visual Studio 2012,並且正在使用C編碼。 – Alec 2014-12-05 07:21:50

回答

3

使用在pthread_create函數我們可以創建線程。

#include<stdio.h> 
    #include<stdlib.h> 
    #include<pthread.h> 

pthread_t tid; pthread_t tid1; void * thrd(void * a) { printf(「thread created \ n」);

printf("%u\n",(unsigned int)tid); 
    } 
    void *thrd1(void *a) 
{ 
    printf("second thread created\n"); 
    printf("%u\n",(unsigned int)tid1); 
    } 

    main() 
    { 

    int a=pthread_create(&tid,NULL,thrd,NULL); 
    int b=pthread_create(&tid1,NULL,thrd1,NULL); 
    sleep(1); 

    }      
1

試試看代碼

 #include<stdio.h> 
     #include<pthread.h> 

     void *hello(void *arg) 
     { 
     printf("thread id=%u\n",(unsigned)pthread_self()); 
     printf("hello welocme Thread created \n"); 
     } 

     main() 
    { 
     pthread_t tid; 

     tid=pthread_create(&tid,NULL,hello,NULL); 


    } 

編譯:

CC FILENAME.C -pthread

相關問題