很多的時候會遇到多線程跑 ,接下來就寫了一個 多線程的demo 廢話不說直接上代碼
#include <iostream>
#include <pthread.h> //多線程相關操作頭文件,可移植眾多平台
using namespace std;
#define NUM_THREADS 5 //線程數
void* say_hello( void* args )
{
int i = *( (int*)args ); //對傳入的參數進行強制類型轉換,由無類型指針轉變為整形指針,再用*讀取其指向到內容
i++;
cout << "i" << i << endl;
cout << "hello..." << endl;
} //函數返回的是函數指針,便於后面作為參數
void* say_hello_w( void* args )
{
int i = *( (int*)args ); //對傳入的參數進行強制類型轉換,由無類型指針轉變為整形指針,再用*讀取其指向到內容
i+2;
cout << "i" << i << endl;
cout << "say_hello_w" << endl;
} //函數返回的是函數指針,便於后面作為參數
void* say_hello_wo( void* args )
{
int i = *( (int*)args ); //對傳入的參數進行強制類型轉換,由無類型指針轉變為整形指針,再用*讀取其指向到內容
i+3;
cout << "i" << i << endl;
cout << "say_hello_wo" << endl;
} //函數返回的是函數指針,便於后面作為參數
void* say_hello_wor( void* args )
{
int i = *( (int*)args ); //對傳入的參數進行強制類型轉換,由無類型指針轉變為整形指針,再用*讀取其指向到內容
i+4;
cout << "i" << i << endl;
cout << "say_hello_wor" << endl;
} //函數返回的是函數指針,便於后面作為參數
int main(int argc, char *argv[])
{
pthread_t tids[NUM_THREADS]; //線程id
// cout << "Hello World!" << endl;
int i=0;
// int ret = pthread_create( &tids[i], NULL, say_hello, NULL ); //參數:創建的線程id,線程參數,線程運行函數的起始地址,運行函數的參數
int ret = pthread_create( &tids[0], NULL, say_hello, (void*)&i ); //傳入到參數必須強轉為void*類型,即無類型指針,&i表示取i的地址,即指向i的指針
if( ret != 0 ) //創建線程成功返回0
{
cout << "pthread_create error:error_code=" << ret << endl;
}
ret = pthread_create( &tids[1], NULL, say_hello_w, (void*)&i ); //傳入到參數必須強轉為void*類型,即無類型指針,&i表示取i的地址,即指向i的指針
if( ret != 0 ) //創建線程成功返回0
{
cout << "pthread_create error:error_code=" << ret << endl;
}
ret = pthread_create( &tids[2], NULL, say_hello_wo, (void*)&i ); //傳入到參數必須強轉為void*類型,即無類型指針,&i表示取i的地址,即指向i的指針
if( ret != 0 ) //創建線程成功返回0
{
cout << "pthread_create error:error_code=" << ret << endl;
}
ret = pthread_create( &tids[3], NULL, say_hello_wor, (void*)&i ); //傳入到參數必須強轉為void*類型,即無類型指針,&i表示取i的地址,即指向i的指針
if( ret != 0 ) //創建線程成功返回0
{
cout << "pthread_create error:error_code=" << ret << endl;
}
pthread_exit( NULL ); //等待各個線程退出后,進程才結束,否則進程強制結束,線程處於未終止的狀態
return 0;
}
在多線程個過程中如果對全局變量進行修改的時候要用互斥鎖。
#include <iostream>
#include <pthread.h>
using namespace std;
#define NUM_THREADS 5
int sum = 0; //定義全局變量,讓所有線程同時寫,這樣就需要鎖機制
pthread_mutex_t sum_mutex; //互斥鎖
void* say_hello( void* args )
{
cout << "hello in thread " << *(( int * )args) << endl;
pthread_mutex_lock( &sum_mutex ); //先加鎖,再修改sum的值,鎖被占用就阻塞,直到拿到鎖再修改sum;
cout << "before sum is " << sum << " in thread " << *( ( int* )args ) << endl;
sum += *( ( int* )args );
cout << "after sum is " << sum << " in thread " << *( ( int* )args ) << endl;
pthread_mutex_unlock( &sum_mutex ); //釋放鎖,供其他線程使用
pthread_exit( 0 );
}
int main()
{
pthread_t tids[NUM_THREADS];
int indexes[NUM_THREADS];
pthread_attr_t attr; //線程屬性結構體,創建線程時加入的參數
pthread_attr_init( &attr ); //初始化
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ); //是設置你想要指定線程屬性參數,這個參數表明這個線程是可以join連接的,join功能表示主程序可以等線程結束后再去做某事,實現了主程序和線程同步功能
pthread_mutex_init( &sum_mutex, NULL ); //對鎖進行初始化
for( int i = 0; i < NUM_THREADS; ++i )
{
indexes[i] = i;
int ret = pthread_create( &tids[i], &attr, say_hello, ( void* )&( indexes[i] ) ); //5個進程同時去修改sum
if( ret != 0 )
{
cout << "pthread_create error:error_code=" << ret << endl;
}
}
pthread_attr_destroy( &attr ); //釋放內存
void *status;
for( int i = 0; i < NUM_THREADS; ++i )
{
int ret = pthread_join( tids[i], &status ); //主程序join每個線程后取得每個線程的退出信息status
if( ret != 0 )
{
cout << "pthread_join error:error_code=" << ret << endl;
}
}
cout << "finally sum is " << sum << endl;
pthread_mutex_destroy( &sum_mutex ); //注銷鎖
}
代碼在 http://pan.baidu.com/s/1jIrEXP8
