1.什么是线程?
定义:在一个程序中多个执行路线就是线程。线程是一个进程内部的一个控制序列。所有进程都有一个执行线程。
区别:当进程执行fork调用时,将创建出该进程的一个副本,新进程由自己的PID,时间调度也是独立的,执行几乎完全独立于父进程。
当创建一个新线程时,新线程拥有自己的栈,但它与创建者共享全局变量,文件描述符,信号处理函数和当前目录状态。
2.第一个线程程序。
创建线程
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
成功返回0失败返回错误代码
终止线程
#include <pthread.h>void pthread_exit(void *retval);终止线程并且返回某个对象的指针。
#include <pthread.h>int pthread_join(pthread_t thread, void **retval);
retval返回线程的返回值。 成功返回0失败返回错误代码
thread1.c:两个线程共享变量,主线程获取另外一个线程的返回值$ vi thread1.c
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>
char message[] = "hello world";
void *thread_function(void *arg)
{
printf("thread_function massage : %s\n",(char*)arg);
sleep(1);
strcpy(message,"OK OK OK");
pthread_exit("thread_function gameover");
}
int main()
{
pthread_t thread;
void *thread_result;
int res = pthread_create(&thread,NULL,thread_function,(void *)message);
if(res != 0)
{
perror("thread create error");
exit(EXIT_FAILURE);
}
res = pthread_join(thread,&thread_result);
if(res != 0)
{
perror("thread_join error");
exit(EXIT_FAILURE);
}
printf("thread was runed\n");
printf("thread_jion returned is %s\n",(char *)thread_result);
printf("now massege is %s\n",message);
exit(EXIT_SUCCESS);
}
$ gcc -o thread1 thread1.c -lpthread
$ ./thread1
thread_function massage : hello world
thread was runed
thread_jion returned is thread_function gameover
now massege is OK OK OK
thread2.c:两个线程同时执行(在两个线程中使用轮询技术)
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>
int run_now = 1;
void *thread_function(void *arg)
{
int count1 = 0;
while(count1<10)
{
if(run_now == 2)
{
printf("thread_function running\n");
run_now = 1;
}
else
{
sleep(1);
}
count1++;
}
pthread_exit("thread_function gameover");
}
int main()
{
pthread_t thread;
void *thread_result;
int res =pthread_create(&thread,NULL,thread_function,(void *)0);
if(res != 0)
{
perror("thread create error");
exit(EXIT_FAILURE);
}
int count2 = 0;
while(count2<10)
{
if(run_now == 1)
{
printf("main running\n");
run_now = 2;
}
else
{
sleep(1);
}
count2++;
}
res = pthread_join(thread,&thread_result);
if(res != 0)
{
perror("thread_join error");
exit(EXIT_FAILURE);
}
printf("thread_jion returned is %s\n",(char *)thread_result);
exit(EXIT_SUCCESS);
}
$ gcc -o thread2 thread2.c -lpthread
$ ./thread2
main running
thread_function running
main running
thread_function running
main running
thread_function running
main running
thread_function running
main running
thread_function running
thread_jion returned is thread_function gameover
thread3.c:一个线程信号量(用信号量进行同步,主线程写,另外一个线程读字符串的长度)
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>
#include<semaphore.h>
#define BUFF_SIZE 1024
char Buff[BUFF_SIZE];
sem_t sem;
void *thread_function(void *arg)
{
while(strncmp(Buff,"end", 3) != 0)
{
sem_wait(&sem);
printf("You input %d characters\n",strlen(Buff)-1);
}
pthread_exit("thread_function gameover");
}
int main()
{
pthread_t thread;
void *thread_result;
int res = sem_init(&sem,0,0);
if(res != 0)
{
perror("sem_init error");
exit(EXIT_FAILURE);
}
res = pthread_create(&thread,NULL,thread_function,(void *)0);
if(res != 0)
{
perror("thread create error");
exit(EXIT_FAILURE);
}
printf("input end to finish\n");
while(strncmp(Buff,"end",3) != 0)
{
fgets(Buff,BUFF_SIZE,stdin);
sem_post(&sem);
}
res = pthread_join(thread,&thread_result);
if(res != 0)
{
perror("thread_join error");
exit(EXIT_FAILURE);
}
printf("thread was runed\n");
printf("thread_jion returned is %s\n",(char *)thread_result);
sem_destroy(&sem);
exit(EXIT_SUCCESS);
}
$ gcc -o thread3 thread3.c -lpthread
$ ./thread3
input end to finish
ni
You input 2 characters
hao
You input 3 characters
meimeida
You input 8 characters
woaini
You input 6 characters
dniejdiejd
You input 10 characters
wo aini chentong
You input 16 characters
end
You input 3 characters
thread was runed
thread_jion returned is thread_function gameover
thread4.c:用互斥量进行同步(一个线程输入数据,一个线程计算数据的大小)
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>
#define BUFF_SIZE 1024
int time_to_exit = 1;
char buff[BUFF_SIZE];
pthread_mutex_t mutex;
void *thread_function(void *arg)
{
sleep(1);
pthread_mutex_lock(&mutex);
while(strncmp(buff,"end",3) != 0)
{
printf("You input %d charcters\n",strlen(buff)-1);
buff[0] = '\0';
pthread_mutex_unlock(&mutex);
sleep(1);
pthread_mutex_lock(&mutex);
while(buff[0] == '\0')
{
pthread_mutex_unlock(&mutex);
sleep(1);
pthread_mutex_lock(&mutex);
}
}
time_to_exit = 0;
buff[0] ='\0';
pthread_mutex_unlock(&mutex);
pthread_exit("thread_function gameover");
}
int main()
{
pthread_t thread;
void *thread_result;
int res = pthread_mutex_init(&mutex,NULL);
if(res != 0)
{
perror("pthread_mutex_init error");
exit(EXIT_FAILURE);
}
res = pthread_create(&thread,NULL,thread_function,(void *)0);
if(res != 0)
{
perror("thread create error");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&mutex);
while(time_to_exit)
{
fgets(buff,BUFF_SIZE,stdin);
pthread_mutex_unlock(&mutex);
while(1)
{
pthread_mutex_lock(&mutex);
if(buff[0] != 0)
{
pthread_mutex_unlock(&mutex);
sleep(1);
}
else
{
break;
}
}
}
pthread_mutex_unlock(&mutex);
printf("thread is finished...\n");
res = pthread_join(thread,&thread_result);
if(res != 0)
{
printf("ppthread_join() error\n");
exit(EXIT_FAILURE);
}
pthread_mutex_destroy(&mutex);
printf("thread_jion returned is %s\n",(char *)thread_result);
exit(EXIT_SUCCESS);
}