文章参考: http://www.cnblogs.com/forstudy/archive/2012/04/05/2433853.html
一. 进程和线程
#include <stdio.h> #include <stdlib.h> #include <pthread.h> int main(){ pthread_t thread_id; thread_id=pthread_self(); // 返回调用线程的线程ID printf("Thread ID: %lu.\n",thread_id); if (pthread_equal(thread_id,pthread_self())) { if (thread_id==0) { printf("Equal!\n"); } else { printf("Not equal!\n"); } return 0; } }
int
pthread_create(pthread_t *tid,
const
pthread_attr_t *attr,(
void
*)(*start_routine)(
void
*),
void
*arg);
参数:
1)tid, 线程标识符
2)attr, 线程属性,通常设置为NULL
3)start_routine, 线程函数
4)arg, 传递给线程函数的参数
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <iostream> void *thrd_func(void *arg); pthread_t tid; int main(){ if (pthread_create(&tid,NULL,thrd_func,NULL)!=0) { printf("Create thread error!\n"); exit(1); } sleep(1); //sleep主线程,保证子线程能够执行成功 printf("TID in pthread_create function: %lu.\n",tid); printf("Main process: PID: %d,TID: %lu.\n",getpid(), pthread_self()); return 0; } void *thrd_func(void *arg){ printf("I am new thread!\n"); printf("New process: PID: %d,TID: %lu.\n",getpid(), pthread_self()); printf("New process: PID: %d,TID: %lu.\n",getpid(), tid); pthread_exit(NULL); return 0; }
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <iostream> void *thrd_func1(void *arg); void *thrd_func2(void *arg); int main(){ pthread_t tid1,tid2; void *tret; // 创建线程tid1,线程函数thrd_func1 if (pthread_create(&tid1,NULL,thrd_func1,NULL)!=0) { printf("Create thread 1 error!\n"); exit(1); } // 创建线程tid2,线程函数thrd_func2 if (pthread_create(&tid2,NULL,thrd_func2,NULL)!=0) { printf("Create thread 2 error!\n"); exit(1); } // 主线程等待线程tid1结束 int retCode = pthread_join(tid1, NULL); printf("Thread 1 exit code:%d\n", retCode); // 主线程等待线程tid2结束 pthread_join(tid2, NULL); printf("Thread 2 exit code:%d\n", retCode); return 0; } void *thrd_func1(void *arg){ printf("Thread 1 returning!\n"); return ((void *)1); // 自动退出线程 } void *thrd_func2(void *arg){ printf("Thread 2 exiting!\n"); pthread_exit((void *)2); // 线程主动退出,返回(void *)2 }
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <iostream> #define THREAD_NUM 3 #define REPEAT_TIMES 5 #define DELAY 4 pthread_mutex_t mutex; void *thrd_func(void *arg); int main(){ pthread_t thread[THREAD_NUM]; int no; void *tret; srand((int)time(0)); //初始化互斥锁 pthread_mutex_init(&mutex,NULL); for(no=0;no<THREAD_NUM;no++){ if (pthread_create(&thread[no],NULL,thrd_func,(void*)no)!=0) { printf("Create thread %d error!\n",no); exit(1); } else { printf("Create thread %d success!\n",no); } //主线程等待子线程结束 for(no=0;no<THREAD_NUM;no++){ if (pthread_join(thread[no],&tret)!=0){ printf("Join thread %d error!\n",no); exit(1); } else { printf("Join thread %d success!\n",no); } } pthread_mutex_destroy(&mutex); return 0; } void *thrd_func(void *arg){ int thrd_num=(int)arg; int delay_time,count; //加锁 if(pthread_mutex_lock(&mutex)!=0) { printf("Thread %d lock failed!\n",thrd_num); pthread_exit(NULL); } printf("Thread %d is starting.\n",thrd_num); for(count=0;count<REPEAT_TIMES;count++) { delay_time=(int)(DELAY*(rand()/(double)RAND_MAX))+1; sleep(delay_time); printf("\tThread %d:job %d delay =%d.\n",thrd_num,count,delay_time); } printf("Thread %d is exiting.\n",thrd_num); //解锁 pthread_mutex_unlock(&mutex); pthread_exit(NULL); }