進程簡單的說就是把一段代碼複製成多份,並讓他們同時執行。進程間通訊是爲了讓他們有序的運行html
線程簡單的說就是讓多個函數同時執行,線程間通訊是爲了讓他們有序的運行app
編譯線程程序時會警告說線程函數找不到函數
pthread 庫不是 Linux 系統默認的庫,鏈接時須要使用靜態庫 libpthread.a,因此在使用pthread_create()建立線程,以及調用 pthread_atfork()函數創建fork處理程序時,須要連接該庫。
問題解決:
在編譯中要加 -lpthread參數
gcc thread.c -o thread -lpthread
thread.c爲你些的源文件,不要忘了加上頭文件#include<pthread.h>post
http://blog.csdn.net/llqkk/article/details/2854558spa
實例1:建立兩個線程,同時執行同一個函數.net
/* ex7-1.c */
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>線程
void print_msg(char *ptr);htm
int main()
{
pthread_t thread1, thread2;
int i,j;
char *msg1="do sth1\n";
char *msg2="do sth2\n";
pthread_create(&thread1,NULL, (void *)(&print_msg), (void *)msg1);
pthread_create(&thread2,NULL, (void *)(&print_msg), (void *)msg2);
sleep(1);
return 0;
}
void print_msg(char *ptr)
{
int retval;
int id=pthread_self();
printf("Thread ID: %x\n",id);
printf("%s",ptr);
pthread_exit(&retval);
}blog
執行gcc ex7-1.c -lpthread進程
./a.out
實例2 建立多個線程執行不一樣函數
代碼來自 http://www.cnblogs.com/BiffoLee/archive/2011/11/18/2254540.html
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10
pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;
void *thread1()
{
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 :主函數在等我完成任務嗎?\n");
pthread_exit(NULL);
}
void *thread2()
{
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 :主函數在等我完成任務嗎?\n");
pthread_exit(NULL);
}
void thread_create(void)
{
int temp;
memset(&thread, 0, sizeof(thread)); //comment1
//建立線程
if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2
printf("線程1建立失敗!\n");
else
printf("線程1被建立\n");
if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3
printf("線程2建立失敗");
else
printf("線程2被建立\n");
}
void thread_wait(void)
{
//等待線程結束
if(thread[0] !=0) { //comment4
pthread_join(thread[0],NULL);
printf("線程1已經結束\n");
}
if(thread[1] !=0) { //comment5
pthread_join(thread[1],NULL);
printf("線程2已經結束\n");
}
}
int main()
{
//用默認屬性初始化互斥鎖
pthread_mutex_init(&mut,NULL);
printf("我是主函數哦,我正在建立線程,呵呵\n");
thread_create();
printf("我是主函數哦,我正在等待線程完成任務阿,呵呵\n");
thread_wait();
return 0;
}
編譯 :
gcc -lpthread -o thread_example lp.c
實例3:信號量控制線程運行順序
/* thread_sem.c */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define THREAD_NUMBER 3
#define REPEAT_NUMBER 3
#define DELAY_TIME_LEVELS 10.0
sem_t sem[THREAD_NUMBER];
void * thrd_func(void *arg)
{
int thrd_num = (int)arg;
int delay_time = 0;
int count = 0;
sem_wait(&sem[thrd_num]);
printf("Thread %d is starting\n", thrd_num);
for (count = 0; count < REPEAT_NUMBER; count++)
{
delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)) + 1;
sleep(delay_time);
printf("\tThread %d: job %d delay = %d\n", thrd_num, count, delay_time);
}
printf("Thread %d finished\n", thrd_num);
pthread_exit(NULL);
}
int main(void)
{
pthread_t thread[THREAD_NUMBER];
int no = 0, res;
void * thrd_ret;
srand(time(NULL));
for (no = 0; no < THREAD_NUMBER; no++)
{
sem_init(&sem[no], 0, 0);
res = pthread_create(&thread[no], NULL, thrd_func, (void*)no);
if (res != 0)
{
printf("Create thread %d failed\n", no);
exit(res);
}
}
printf("Create treads success\n Waiting for threads to finish...\n");
sem_post(&sem[THREAD_NUMBER - 1]);
for (no = THREAD_NUMBER - 1; no >= 0; no--)
{
res = pthread_join(thread[no], &thrd_ret);
if (!res)
{
printf("Thread %d joined\n", no);
}
else
{
printf("Thread %d join failed\n", no);
}
sem_post(&sem[(no + THREAD_NUMBER - 1) % THREAD_NUMBER]);
}
for (no = 0; no < THREAD_NUMBER; no++)
{
sem_destroy(&sem[no]);
}
return 0;
}
實例4:互斥鎖的使用
在這個程序中,一個線程要往緩衝區寫數據,另外一個線程要讀數據,每次只能讓一個線程操做緩衝區
/*ex7-3.c*/
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define FALSE 0
#define TRUE 1
void readfun();
void writefun();
char buffer[256];
int buffer_has_item=0;
int retflag=FALSE,i=0;
pthread_mutex_t mutex;
int main()
{
void *retval;
pthread_t reader;
pthread_mutex_init(&mutex,NULL);
pthread_create(&reader,NULL,(void *)&readfun,NULL);
writefun();
pthread_join(reader,&retval);
}
void readfun()
{
while(1)
{
if(retflag)
return;
pthread_mutex_lock(&mutex);
if(buffer_has_item==1)
{
printf("%s",buffer);
buffer_has_item=0;
}
pthread_mutex_unlock(&mutex);
}
}
void writefun()
{
int i=0;
while(1)
{
if(i==10)
{
retflag=TRUE;
return;
}
pthread_mutex_lock(&mutex);
if(buffer_has_item==0)
{
sprintf(buffer,"This is %d\n",i++);
buffer_has_item=1;
}
pthread_mutex_unlock(&mutex);
}
}
實例5 條件變量
text
[cpp] view plain copy