⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread_mutex.c

📁 pthread的例子程序
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#define THREAD_NUM 8void *functionC();pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;int  counter = 0;main(){   pthread_t thread_id[THREAD_NUM];   int i;   /* Create independant threads each of which will execute functionC */   for (i = 0 ; i < THREAD_NUM ; i++){	   pthread_create(&thread_id[i], NULL, &functionC, NULL);   }   /* Wait till threads are complete before main continues. Unless we  */   /* wait we run the risk of executing an exit which will terminate   */   /* the process and all threads before the threads have completed.   */   for (i = 0 ; i < THREAD_NUM ; i++){	   pthread_join(thread_id[i], NULL);   }   exit(0);}void *functionC(){   int s_counter;   //pthread_mutex_lock( &mutex1 );   counter++;   sleep(1);   s_counter = counter * counter;   printf("Counter value: %d\n", s_counter);   //pthread_mutex_unlock( &mutex1 );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -