pthread线程编程常用API
来源:互联网 发布:淘宝白酒真假 编辑:程序博客网 时间:2024/05/20 15:12
<pre name="code" class="cpp"><p>自己写共享控制</p><p>#include <stdio.h></p>#include <unistd.h>#include <stdlib.h>#include <pthread.h>//基本的共享变量交互void *thread_function(void *arg);int run_now = 1;char message[] = "Hello World";int main() { int res; pthread_t a_thread; void *thread_result; int print_count1 = 0; res = pthread_create(&a_thread, NULL, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } while(print_count1++ < 20) { if (run_now == 1) { printf("1"); run_now = 2; } else { sleep(1); } } printf("\nWaiting for thread to finish...\n"); res = pthread_join(a_thread, &thread_result); if (res != 0) { perror("Thread join failed"); exit(EXIT_FAILURE); } printf("Thread joined\n"); exit(EXIT_SUCCESS);}void *thread_function(void *arg) { int print_count2 = 0; while(print_count2++ < 20) { if (run_now == 2) { printf("2"); run_now = 1; } else { sleep(1); } } sleep(3);}
信号量的使用
<pre name="code" class="cpp">#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <semaphore.h>void *thread_function(void *arg);sem_t bin_sem;#define WORK_SIZE 1024char work_area[WORK_SIZE];int main() { int res; pthread_t a_thread; void *thread_result; res = sem_init(&bin_sem, 0, 0); if (res != 0) { perror("Semaphore initialization failed"); exit(EXIT_FAILURE); } res = pthread_create(&a_thread, NULL, thread_function, NULL); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } printf("Input some text. Enter 'end' to finish\n"); while(strncmp("end", work_area, 3) != 0) { fgets(work_area, WORK_SIZE, stdin); sem_post(&bin_sem); } printf("\nWaiting for thread to finish...\n"); res = pthread_join(a_thread, &thread_result); if (res != 0) { perror("Thread join failed"); exit(EXIT_FAILURE); } printf("Thread joined\n"); sem_destroy(&bin_sem); exit(EXIT_SUCCESS);}void *thread_function(void *arg) { sem_wait(&bin_sem); while(strncmp("end", work_area, 3) != 0) { printf("You input %d characters\n", strlen(work_area) -1); sem_wait(&bin_sem); } pthread_exit(NULL);}
互斥量的使用
<pre name="code" class="cpp">#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <semaphore.h>void *thread_function(void *arg);pthread_mutex_t work_mutex; /* protects both work_area and time_to_exit */#define WORK_SIZE 1024char work_area[WORK_SIZE];int time_to_exit = 0;int main() { int res; pthread_t a_thread; void *thread_result; res = pthread_mutex_init(&work_mutex, NULL); if (res != 0) { perror("Mutex initialization failed"); exit(EXIT_FAILURE); } res = pthread_create(&a_thread, NULL, thread_function, NULL); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } pthread_mutex_lock(&work_mutex); printf("Input some text. Enter 'end' to finish\n"); while(!time_to_exit) { fgets(work_area, WORK_SIZE, stdin); pthread_mutex_unlock(&work_mutex); while(1) { pthread_mutex_lock(&work_mutex); if (work_area[0] != '\0') { pthread_mutex_unlock(&work_mutex); sleep(1); } else { break; } } } pthread_mutex_unlock(&work_mutex); printf("\nWaiting for thread to finish...\n"); res = pthread_join(a_thread, &thread_result); if (res != 0) { perror("Thread join failed"); exit(EXIT_FAILURE); } printf("Thread joined\n"); pthread_mutex_destroy(&work_mutex); exit(EXIT_SUCCESS);}void *thread_function(void *arg) { sleep(1); pthread_mutex_lock(&work_mutex); while(strncmp("end", work_area, 3) != 0) { printf("You input %d characters\n", strlen(work_area) -1); work_area[0] = '\0'; pthread_mutex_unlock(&work_mutex); sleep(1); pthread_mutex_lock(&work_mutex); while (work_area[0] == '\0' ) { pthread_mutex_unlock(&work_mutex); sleep(1); pthread_mutex_lock(&work_mutex); } } time_to_exit = 1; work_area[0] = '\0'; pthread_mutex_unlock(&work_mutex); pthread_exit(0);}
条件变量的使用
#include <pthread.h> #include <stdio.h> #include <stdlib.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/ pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化条件变量*/ void *thread1(void *); void *thread2(void *); int i=1; int main(void) { pthread_t t_a; pthread_t t_b; pthread_create(&t_a,NULL,thread1,(void *)NULL);/*创建进程t_a*/ pthread_create(&t_b,NULL,thread2,(void *)NULL); /*创建进程t_b*/ pthread_join(t_a, NULL);/*等待进程t_a结束*/ pthread_join(t_b, NULL);/*等待进程t_b结束*/ pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); exit(0); } void *thread1(void *junk) { for(i=1;i<=6;i++) { pthread_mutex_lock(&mutex);/*锁住互斥量*/ printf("thread1: lock %d/n", __LINE__); if(i%3==0){ printf("thread1:signal 1 %d/n", __LINE__); pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/ printf("thread1:signal 2 %d/n", __LINE__); sleep(1); } pthread_mutex_unlock(&mutex);/*解锁互斥量*/ printf("thread1: unlock %d/n/n", __LINE__); sleep(1); } } void *thread2(void *junk) { while(i<6) { pthread_mutex_lock(&mutex); printf("thread2: lock %d/n", __LINE__); if(i%3!=0){ printf("thread2: wait 1 %d/n", __LINE__); pthread_cond_wait(&cond,&mutex);/*解锁mutex,并等待cond改变*/ printf("thread2: wait 2 %d/n", __LINE__); } pthread_mutex_unlock(&mutex); printf("thread2: unlock %d/n/n", __LINE__); sleep(1); } }
线程属性的设置
<pre name="code" class="cpp">#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <pthread.h>void *thread_function(void *arg);char message[] = "Hello World";int thread_finished = 0;int main() { int res; pthread_t a_thread; void *thread_result; pthread_attr_t thread_attr; res = pthread_attr_init(&thread_attr); if (res != 0) { perror("Attribute creation failed"); exit(EXIT_FAILURE); } res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); if (res != 0) { perror("Setting detached attribute failed"); exit(EXIT_FAILURE); } res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } // while(!thread_finished) { printf("Waiting for thread to say it's finished...\n"); sleep(1); } (void)pthread_attr_destroy(&thread_attr); printf("Other thread finished, bye!\n"); exit(EXIT_SUCCESS);}void *thread_function(void *arg) { printf("thread_function is running. Argument was %s\n", (char *)arg); sleep(4); printf("Second thread setting finished flag, and exiting now\n"); thread_finished = 1; pthread_exit(NULL);}
多线程的管理
<pre name="code" class="cpp">#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <pthread.h>#define NUM_THREADS 6 //创建6个线程void *thread_function(void *arg);int main() { int res; pthread_t a_thread[NUM_THREADS]; void *thread_result; int lots_of_threads; for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++) { res = pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function, (void *)&lots_of_threads); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } sleep(1); } printf("Waiting for threads to finish...\n"); for(lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--) { //从后往前join res = pthread_join(a_thread[lots_of_threads], &thread_result); if (res == 0) { printf("Picked up a thread\n"); } else { perror("pthread_join failed"); } } printf("All done\n"); exit(EXIT_SUCCESS);}void *thread_function(void *arg) { int my_number = *(int *)arg; int rand_num; printf("thread_function is running. Argument was %d\n", my_number); rand_num=1+(int)(9.0*rand()/(RAND_MAX+1.0)); //1到10之间 //printf("RAND_MAX:%d\n", RAND_MAX); sleep(rand_num); printf("Bye from %d\n", my_number); pthread_exit(NULL);}
线程的局部存储
<pre name="code" class="cpp">#include <stdio.h>#include <pthread.h>//#include <malloc.h>#include <stdlib.h>static pthread_key_t thread_log_key;void write_to_thread_log( char const * message ){ FILE * thread_log = ( FILE * )pthread_getspecific( thread_log_key ); fprintf( thread_log, "%s/n", message );}void close_thread_log( void * thread_log ){ fclose( ( FILE * )thread_log );}void * thread_function( void * args ){ char thread_log_filename[ 20 ]; FILE* thread_log; sprintf( thread_log_filename, "thread%d.log", ( int )pthread_self() ); thread_log = fopen( thread_log_filename, "w" ); pthread_setspecific( thread_log_key, thread_log ); write_to_thread_log( "Thread starting." ); return NULL;}int main(){ int i; pthread_t threads[5]; pthread_key_create( &thread_log_key, close_thread_log ); for ( i = 0; i < 5; ++i ) { pthread_create( &(threads[i]), NULL, thread_function, NULL ); } for ( i = 0; i < 5; ++i ) { pthread_join( threads[i], NULL ); } //注意close_thread_log将会在各个线程结束的时候执行,和delete无关。 pthread_key_delete(thread_log_key); return 0;}
0 0
- pthread线程编程常用API
- Unix线程基础编程pthread
- pthread线程属性编程示例
- pthread线程编程--POSIX的线程机制
- pthread常用API及简单介绍
- linux线程(pthread)编程中的segmentation fault!!
- UNIX/LINUX编程学习之线程pthread
- 多线程编程之终止pthread线程
- Pthread编程-设置/获取线程属性
- pthread API
- pthread线程
- pthread线程
- pthread线程
- 线程pthread
- Pthread线程
- pthread 线程库编程 一:线程与互斥锁 mutex_lock
- pthread学习笔记(二)--pthread常用API及简单介绍
- PThread编程
- vs中bitmap等图标文件你在哪?
- poj 1056 Trie树判断哈夫曼编码是否合法
- FreeMarker整合Spring 3
- 2014年腾讯游戏实习生面试体验
- 汇编入门学习笔记 (三) —— 第一个程序
- pthread线程编程常用API
- Unity3d Resources TextAsset 文本
- TABLE ACCESS BY INDEX ROWID前面的星号
- 用位操作实现的数据交换
- POJ 3905 Perfect Election(简单2-SAT)
- 天降噩耗
- jvm基础理解
- 解决VTune错误PMU resources currently being used by another profiling tool or process
- java web web.xml 中的url-pattern 和 springMVC