日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java多线程w3c_多线程

發布時間:2024/4/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java多线程w3c_多线程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

多線程

多線程是多任務處理的一種特殊形式,而多任務處理是一種讓你的電腦能并發運行兩個或兩個以上程序的特性。一般有兩種類型的多任務處理:基于進程的和基于線程的。

基于進程的多任務處理是并發執行的程序?;诰€程的多任務處理是并發執行的程序的一部分。

多線程程序包含了可以并發運行的兩個或更多個程序部分。這樣程序中的每個部分稱為一個線程,并且每個線程都定義了一個單獨的執行路徑。

C++ 不包含對多線程應用程序的任何嵌入式支持。相反,它完全依賴于操作系統來提供此項功能。

本教程假設您正在使用的是 Linux 操作系統,我們將要使用 POSIX 編寫 C++ 多線程程序。 POSIX 線程,或稱 Pthreads,它提供了在許多類 Unix 的 POSIX 系統(如 FreeBSD,NetBSD,GNU/Linux,Mac OS X 和 Solaris)中可用的 API。

創建線程

我們使用下面的函數來創建一個 POSIX 線程:

#include

pthread_create (thread, attr, start_routine, arg)

這里的 pthread_create 創建了一個新線程,并使其可執行。這個函數可以在代碼中的任意位置調用任意次。

下面是詳細的參數說明:

參數

描述

thread

新線程的不透明、唯一的標識符,它由子函數返回。

attr

一個不透明的屬性對象,可用于設置線程屬性。你可以指定一個線程的屬性對象,默認值為 NULL。

start_routine

C++ 例程,線程一旦創建將會被執行。

arg

一個傳遞給 start_routine 的參數。它必須傳遞一個 void 類型指針的引用。如果沒有參數傳遞,默認值為 NULL。

一個進程可創建的最大線程數是依賴實現決定的。線程一旦創建,它們之間是對等的,而且也有可能創建其它的線程。線程之間沒有隱含的層次或依賴關系。

終止線程

我們使用下面的函數來終止一個 POSIX 線程:

#include

pthread_exit (status)

此處的 pthread_exit 用于顯式的退出一個線程。通常在線程已完成了其工作,并且沒有存在的必要的時候,調用 pthread_exit()函數。

如果 main()在其創建的線程之前終止,并且使用了 pthread_exit() 來退出線程,那么其線程將會繼續執行。否則,當 main() 終止后,這些線程將會自動終止。

例子

下面簡單的樣例代碼,用 pthread_create() 函數創建了 5 個線程。每個線程均打印 “Hello World!”,然后調用 pthread_exit() 函數終止了線程。

#include

#include

#include

using namespace std;

#define NUM_THREADS 5

void *PrintHello(void *threadid)

{

long tid;

tid = (long)threadid;

cout << "Hello World! Thread ID, " << tid << endl;

pthread_exit(NULL);

}

int main ()

{

pthread_t threads[NUM_THREADS];

int rc;

int i;

for( i=0; i < NUM_THREADS; i++ ){

cout << "main() : creating thread, " << i << endl;

rc = pthread_create(&threads[i], NULL,

PrintHello, (void *)i);

if (rc){

cout << "Error:unable to create thread," << rc << endl;

exit(-1);

}

}

pthread_exit(NULL);

}

使用 -lpthread 庫編譯上面的程序,如下所示:

$gcc test.cpp -lpthread

現在執行上面的程序,將會產生如下的結果:

main() : creating thread, 0

main() : creating thread, 1

main() : creating thread, 2

main() : creating thread, 3

main() : creating thread, 4

Hello World! Thread ID, 0

Hello World! Thread ID, 1

Hello World! Thread ID, 2

Hello World! Thread ID, 3

Hello World! Thread ID, 4

傳遞參數給線程

下面的例子展示了如何通過一個結構體傳遞多個參數。你可以在一個線程回調中傳遞任何數據類型,這是因為它指向 void 類型。

下面的例子解釋了這一點:

#include

#include

#include

using namespace std;

#define NUM_THREADS 5

struct thread_data{

int thread_id;

char *message;

};

void *PrintHello(void *threadarg)

{

struct thread_data *my_data;

my_data = (struct thread_data *) threadarg;

cout << "Thread ID : " << my_data->thread_id ;

cout << " Message : " << my_data->message << endl;

pthread_exit(NULL);

}

int main ()

{

pthread_t threads[NUM_THREADS];

struct thread_data td[NUM_THREADS];

int rc;

int i;

for( i=0; i < NUM_THREADS; i++ ){

cout <

td[i].thread_id = i;

td[i].message = "This is message";

rc = pthread_create(&threads[i], NULL,

PrintHello, (void *)&td[i]);

if (rc){

cout << "Error:unable to create thread," << rc << endl;

exit(-1);

}

}

pthread_exit(NULL);

}

當上述代碼編譯和執行后,將會有以下的結果:

main() : creating thread, 0

main() : creating thread, 1

main() : creating thread, 2

main() : creating thread, 3

main() : creating thread, 4

Thread ID : 3 Message : This is message

Thread ID : 2 Message : This is message

Thread ID : 0 Message : This is message

Thread ID : 1 Message : This is message

Thread ID : 4 Message : This is message

連接和分離線程

下面的兩個函數,我們可以用它們來連接或分離線程:

pthread_join (threadid, status)

pthread_detach (threadid)

pthread_join()子例程會阻塞調用它的線程,一直等到其指定的 threadid 的線程結束為止。當一個線程創建后,它的屬性決定了它是否是可連接的或可分離的。只有創建時屬性為可連接的線程才可以連接。如果創建的是一個可分離的線程,那么它永遠不能連接。

下面的例子演示了如何使用 pthread_join 函數來等待一個線程結束。

#include

#include

#include

#include

using namespace std;

#define NUM_THREADS 5

void *wait(void *t)

{

int i;

long tid;

tid = (long)t;

sleep(1);

cout << "Sleeping in thread " << endl;

cout << "Thread with id : " << tid << " ...exiting " << endl;

pthread_exit(NULL);

}

int main ()

{

int rc;

int i;

pthread_t threads[NUM_THREADS];

pthread_attr_t attr;

void *status;

// Initialize and set thread joinable

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

for( i=0; i < NUM_THREADS; i++ ){

cout << "main() : creating thread, " << i << endl;

rc = pthread_create(&threads[i], NULL, wait, &&i );

if (rc){

cout << "Error:unable to create thread," << rc << endl;

exit(-1);

}

}

// free attribute and wait for the other threads

pthread_attr_destroy(&attr);

for( i=0; i < NUM_THREADS; i++ ){

rc = pthread_join(threads[i], &status);

if (rc){

cout << "Error:unable to join," << rc << endl;

exit(-1);

}

cout << "Main: completed thread id :" << i ;

cout << " exiting with status :" << status << endl;

}

cout << "Main: program exiting." << endl;

pthread_exit(NULL);

}

當上述代碼編譯和執行后,將產生以下的結果:

main() : creating thread, 0

main() : creating thread, 1

main() : creating thread, 2

main() : creating thread, 3

main() : creating thread, 4

Sleeping in thread

Thread with id : 0 .... exiting

Sleeping in thread

Thread with id : 1 .... exiting

Sleeping in thread

Thread with id : 2 .... exiting

Sleeping in thread

Thread with id : 3 .... exiting

Sleeping in thread

Thread with id : 4 .... exiting

Main: completed thread id :0 exiting with status :0

Main: completed thread id :1 exiting with status :0

Main: completed thread id :2 exiting with status :0

Main: completed thread id :3 exiting with status :0

Main: completed thread id :4 exiting with status :0

Main: program exiting.

總結

以上是生活随笔為你收集整理的java多线程w3c_多线程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。