1. 编写一个基本的多线程程序(主线程里面创建一个子线程)
/*************************************************************************
> File Name: 1_homework.c
> Author: 梁惠涌
> Addr:
> Created Time: 2015年04月22日 星期三 15时22分34秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include <semaphore.h> //sem_t 的声明
void *fun1(void *arg){
pthread_detach( pthread_self() ); //与主线程分离
while(1){
*((int *)arg) += 2;
printf("fun1 the num is: %d \n",*((int*)arg));
sleep(1);
}
pthread_exit(0); //主线程结束
}
int main(){
int num=3, ret1;
pthread_t thread1;
ret1 = pthread_create(&thread1, NULL, fun1, (void *)&num);
if(ret1 != 0){
perror("pthread_create1");
exit(0);
}
sleep(1);
printf("=================== main end ================\n");
pthread_exit(0); //主线程结束
}
2. 把上面的程序改为子线程给父线程传递返回值
/*************************************************************************
> File Name: 2_homework.c
> Author: 梁惠涌
> Addr:
> Created Time: 2015年04月22日 星期三 15时39分49秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
void *fun1(void *arg){
int num=100;
printf("---------------pthread--------------\n");
printf("the num is: %d \n", num);
printf("--------------- end --------------\n");
pthread_exit((void *)num); //主线程结束
}
int main(){
int ret1, num;
pthread_t thread1;
ret1 = pthread_create(&thread1, NULL, fun1, NULL);
if(ret1 != 0){
perror("pthread_create1");
exit(0);
}
pthread_join(thread1,(void *)&num);
printf("--------------- main --------------\n");
printf("return the num is: %d \n", (int )num);
printf("--------------- end --------------\n");
pthread_exit(0); //主线程结束
}
3. 用多线程写一个程序,实现对数据的读写(可以是一个全局变量或其他的数据)
/*************************************************************************
> File Name: 3_homework.c
> Author: 梁惠涌
> Addr:
> Created Time: 2015年04月22日 星期三 15时53分00秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include <semaphore.h> //sem_t 的声明
void *fun1(void *arg){
pthread_detach( pthread_self() ); //与主线程分离
while(1){
*((int *)arg) += 2;
printf("the num write: %d \n",*((int*)arg));
sleep(1);
}
pthread_exit(0); //主线程结束
}
void *fun2(void *arg){
pthread_detach( pthread_self() ); //与主线程分离
while(1){
printf("the num read is: %d \n",*((int*)arg));
sleep(1);
}
pthread_exit(0); //主线程结束
}
int main(){
int num=0, ret1;
pthread_t thread1, thread2;
ret1 = pthread_create(&thread1, NULL, fun1, (void *)&num);
if(ret1 != 0){
perror("pthread_create1");
exit(0);
}
ret1 = pthread_create(&thread2, NULL, fun2, (void *)&num);
if(ret1 != 0){
perror("pthread_create2");
exit(0);
}
sleep(1);
printf("=================== main end ================\n");
pthread_exit(0); //主线程结束
}
4. 分别用信号量和互斥锁实现第3个作业的读写互斥
/*************************************************************************
> File Name: 4_homework.c
> Author: 梁惠涌
> Addr:
> Created Time: 2015年04月22日 星期三 15时57分54秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include <semaphore.h> //sem_t 的声明
sem_t sem; //信号量声明
pthread_mutex_t mutex; //互斥锁声明
void *fun1(void *arg){
pthread_detach( pthread_self() ); //与主线程分离
while(1){
sem_wait(&sem);
*((int *)arg) += 2;
printf("==信号量== num write: %d \n",*((int*)arg));
sleep(1);
sem_post(&sem);
}
pthread_exit(0); //主线程结束
}
void *fun2(void *arg){
pthread_detach( pthread_self() ); //与主线程分离
while(1){
sem_wait(&sem);
printf("==信号量== num read is: %d \n",*((int*)arg));
sleep(1);
sem_post(&sem);
}
pthread_exit(0); //主线程结束
}
void *fun3(void *arg){
pthread_detach( pthread_self() ); //与主线程分离
while(1){
pthread_mutex_lock(&mutex);
*((int *)arg) += 2;
printf(">>互斥锁>> num write: %d \n",*((int*)arg));
sleep(1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(0); //主线程结束
}
void *fun4(void *arg){
pthread_detach( pthread_self() ); //与主线程分离
while(1){
pthread_mutex_lock(&mutex);
printf(">>互斥锁>> num read is: %d \n",*((int*)arg));
sleep(1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(0); //主线程结束
}
int main(){
//------------------------信号量互斥--------------------
int num1=0, ret1;
pthread_t thread1, thread2;
sem_init(&sem, 0, 1);
ret1 = pthread_create(&thread1, NULL, fun1, (void *)&num1);
if(ret1 != 0){
perror("pthread_create1");
exit(0);
}
ret1 = pthread_create(&thread2, NULL, fun2, (void *)&num1);
if(ret1 != 0){
perror("pthread_create2");
exit(0);
}
//------------------------互斥锁互斥--------------------
int num2=1000, ret2;
pthread_t thread_1, thread_2;
pthread_mutex_init(&mutex, NULL);
ret2 = pthread_create(&thread_1, NULL, fun3, (void *)&num2);
if(ret2!= 0){
perror("pthread_create1");
exit(0);
}
ret1 = pthread_create(&thread_2, NULL, fun4, (void *)&num2);
if(ret2 != 0){
perror("pthread_create2");
exit(0);
}
printf("=================== main end ================\n");
pthread_exit(0); //主线程结束
}
5. 用信号量实现第3个作业的读写同步
/*************************************************************************
> File Name: 5_homework.c
> Author: 梁惠涌
> Addr:
> Created Time: 2015年04月22日 星期三 16时36分01秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include <semaphore.h> //sem_t 的声明
sem_t w_sem, r_sem; //信号量声明
void *fun1(void *arg){
pthread_detach( pthread_self() ); //与主线程分离
while(1){
sem_wait(&w_sem);
*((int *)arg) += 2;
printf(">>>> num write: %d \n",*((int*)arg));
sleep(1);
sem_post(&r_sem);
}
pthread_exit(0); //主线程结束
}
void *fun2(void *arg){
pthread_detach( pthread_self() ); //与主线程分离
while(1){
sem_wait(&r_sem);
printf(">>>> num read is: %d \n",*((int*)arg));
sleep(1);
sem_post(&w_sem);
}
pthread_exit(0); //主线程结束
}
int main(){
//------------------------信号量互斥--------------------
int num1=0, ret1;
pthread_t thread1, thread2;
sem_init(&w_sem, 0, 1);
sem_init(&r_sem, 0, 0);
ret1 = pthread_create(&thread1, NULL, fun1, (void *)&num1);
if(ret1 != 0){
perror("pthread_create1");
exit(0);
}
ret1 = pthread_create(&thread2, NULL, fun2, (void *)&num1);
if(ret1 != 0){
perror("pthread_create2");
exit(0);
}
printf("=================== main end ================\n");
pthread_exit(0); //主线程结束
}
原文:http://blog.csdn.net/muyang_ren/article/details/45250551