http://blog.csdn.net/kennyrose/article/details/7545424
2012
- #include<pthread.h>
- #include<stdio.h>
- #include<unistd.h>
- #include<string.h>
-
- #define MAX 10
-
- pthread_t thread[2];
- pthread_mutex_t mut;
- int number = 0;
- int i;
-
- void* thread1( void *param )
- {
- printf( "thread1: I'm thread 1\n " );
- for( i = 0; i < MAX; i++ )
- {
- printf( "thread1: number = %d\n", number );
- pthread_mutex_lock( &mut );
- ++number;
- pthread_mutex_unlock( &mut );
- sleep( 2 );
- }
-
- printf( "thread1: 主函數(shù)等我完成任務(wù)嗎? \n " );
- pthread_exit( NULL );
-
- return ( void* )0;
- }
-
- void* thread2( void* param )
- {
- printf( "thread2: I'm thread 2\n" );
- for( i = 0; i < MAX; i++ )
- {
- printf( "thread2: number = %d\n", number );
- pthread_mutex_lock( &mut );
- ++number;
- pthread_mutex_unlock( &mut );
- sleep( 3 );
- }
-
- printf( "thread2: 主函數(shù)在等我完成任務(wù)嗎? \n" );
- pthread_exit( NULL );
-
- return ( void* )0;
- }
-
- void thread_create( void )
- {
- int temp;
- memset( &thread, 0, sizeof( thread ) );
-
- // 創(chuàng)建線程1
- if( ( temp = pthread_create( &thread[0], NULL, thread1, NULL ) ) != 0 )
- {
- printf( "線程1創(chuàng)建失敗!\n" );
- }
- else
- {
- printf( "線程1被創(chuàng)建\n" );
- }
-
- // 創(chuàng)建線程2
- if( ( temp = pthread_create( &thread[1], NULL, thread2, NULL ) ) != 0 )
- {
- printf( "線程2創(chuàng)建失敗!\n" );
- }
- else
- {
- printf( "線程2被創(chuàng)建\n" );
- }
- }
-
- void thread_wait( void )
- {
- // 主函數(shù)等待線程1結(jié)束
- if ( thread[0] != 0 )
- {
- pthread_join( thread[0], NULL );
- printf( "線程1已經(jīng)結(jié)束\n" );
- }
-
- // 主函數(shù)等待線程2結(jié)束
- if ( thread[1] != 0 )
- {
- pthread_join( thread[1], NULL );
- printf( "線程2已經(jīng)結(jié)束\n" );
- }
- }
-
- int main()
- {
- // 初始化互斥鎖
- pthread_mutex_init( &mut, NULL );
-
- printf( "我是main函數(shù),正在創(chuàng)建線程...\n" );
- thread_create();
-
- printf( "我是主函數(shù),正在等待線程完成任務(wù)...\n" );
- thread_wait();
-
- printf( "main函數(shù)已退出\n" );
- return 0;
- }
// main output1
- 我是main函數(shù),正在創(chuàng)建線程...
- 線程1被創(chuàng)建
- 線程2被創(chuàng)建
- 我是主函數(shù),正在等待線程完成任務(wù)...
- thread1: I'm thread 1
- thread2: I'm thread 2
- thread1: number = 0
- thread2: number = 0
- thread1: number = 2
- thread2: number = 3
- thread1: number = 4
- thread2: number = 5
- thread1: number = 6
- thread1: number = 7
- thread2: number = 8
- thread1: number = 9
- thread2: number = 10
- thread1: 主函數(shù)等我完成任務(wù)嗎?
- 線程1已經(jīng)結(jié)束
- thread2: 主函數(shù)在等我完成任務(wù)嗎?
- 線程2已經(jīng)結(jié)束
- main函數(shù)已退出
// main output2
- 我是main函數(shù),正在創(chuàng)建線程...
- 線程1被創(chuàng)建
- 線程2被創(chuàng)建
- 我是主函數(shù),正在等待線程完成任務(wù)...
- thread1: I'm thread 1
- thread1: number = 0
- thread2: I'm thread 2
- thread2: number = 1
- thread1: number = 2
- thread2: number = 3
- thread1: number = 4
- thread2: number = 5
- thread1: number = 6
- thread1: number = 7
- thread2: number = 8
- thread1: number = 9
- thread2: number = 10
- thread1: 主函數(shù)等我完成任務(wù)嗎?
- 線程1已經(jīng)結(jié)束
- thread2: 主函數(shù)在等我完成任務(wù)嗎?
- 線程2已經(jīng)結(jié)束
- main函數(shù)已退出
g++編譯命令如下: g++ -lpthread -o helloworld.out helloworld.cpp
問題:兩個(gè)線程共同完成任務(wù),但是對(duì)于
共享變量 i 沒有互斥加鎖操作,
所以以上多線程程序還是存在問題的!
為了達(dá)到兼容可移植的目的,POSIX給出了C語言下的多線程編程庫 pthread,具體的學(xué)習(xí)資料請(qǐng)參考一下鏈接
http://blog.csdn.net/future_fighter/article/details/3865071
http://blog.csdn.net/sunboy_2050/article/details/5921003
http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/index.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。