日期:2014-05-16 浏览次数:20776 次
#include<stdio.h> #include<pthread.h> int x=1,sum; pthread_mutex_t m_CS; void sumx(void) { pthread_mutex_lock( &m_CS ); int i=0; printf("进程1开始\n"); for(i=0;i<1000000000;i++) { sum=sum+x; } printf("sum=%d\n",sum); pthread_mutex_unlock( &m_CS ); } void setx(void) { pthread_mutex_lock( &m_CS ); printf("线程2开始"); x=0; printf("线程2结束"); pthread_mutex_unlock( &m_CS ); } int main() { pthread_mutex_init( &m_CS, NULL ); //之后创建2个线程 //线程都结束后 pthread_mutex_destroy( &m_CS ); return 0; }