今天学习一下Linux下多线程的开发,这在Android开发中很常见,也学习一下线程同步的实现。第一步就来创建线程的实现
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
void* fun1(void* arg)
{
while(1)
{
printf("thread1 function...\n");
sleep(1);
}
}
int main(int arg,char* args[])
{
pthread_t thread1,thread2;
if( pthread_create(&thread1,NULL,fun1,NULL) != 0 )
{
printf("%s\n",strerror(errno));
}
while(1)
{
sleep(1);
}
return EXIT_SUCCESS;
}
~
makefile文件需要引用libpthread.so库
.SUFFIXES:.c .o
CC=gcc
SRC1=ThreadTestDemo7.c
OBJEXEC1=$(SRC1:.c=.o)
EXEC1=ThreadTestDemo7
all: $(OBJEXEC1)
$(CC) -o $(EXEC1) $(OBJEXEC1) -lpthread
@echo '-----------------------ok--------------------'
.c.o:
$(CC) -o $@ -c $<
echoall:
echo "my name is shaozhongqi"
clean:
rm -rf $(OBJEXEC)
~
~
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
void* fun1(void* arg)
{
int id = *(int*)arg;
while(1)
{
printf("thread%d function...\n",id);
sleep(1);
}
}
int main(int arg,char* args[])
{
pthread_t thread1,thread2;
int id1 = 1;
int id2 = 2;
if( pthread_create(&thread1,NULL,fun1,&id1) != 0 )
{
printf("%s\n",strerror(errno));
}
if( pthread_create(&thread2,NULL,fun1,&id2) != 0)
{
printf("%s\n",strerror(errno));
}
while(1)
{
sleep(1);
}
return EXIT_SUCCESS;
}
~
主要是学习pthread_create()后面的参数 &id1就是对应回调函数里面的void* arg;
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
void* fun1(void* arg)
{
int i = 0;
int id = *(int*)arg;
for(i = 0 ; i < 15; i++)
{
printf("thread%d function...\n",id);
sleep(1);
}
printf("thread%d end\n",id);
}
int main(int arg,char* args[])
{
pthread_t thread1,thread2;
int id1 = 1;
int id2 = 2;
if( pthread_create(&thread1,NULL,fun1,&id1) != 0 )
{
printf("%s\n",strerror(errno));
}
if( pthread_create(&thread2,NULL,fun1,&id2) != 0)
{
printf("%s\n",strerror(errno));
}
pthread_join(thread1,NULL);
return EXIT_SUCCESS;
}
pthread_join()就是在thread1执行结束以后再执行下面的方法 所以main进程会等待thread1线程结束后在执行
include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
void* fun1(void* arg)
{
int* p = malloc(sizeof(int));
*p = 111;
return p;
}
int main(int arg,char* args[])
{
pthread_t thread1,thread2;
int id1 = 1;
int id2 = 2;
if( pthread_create(&thread1,NULL,fun1,&id1) != 0 )
{
printf("%s\n",strerror(errno));
}
int* p = NULL;
// pthread_join(thread1,(void*)&p);
pthread_detach(thread1);
printf("return value:%d\n",*p);
return EXIT_SUCCESS;
}
~
~
~
~
pthread_join(thread1,(void**)&p)会获得thread1的返回值 !!!同时Linux下还会可分离线程的概念:
一旦线程成为可分离线程之后,就不能再使用pthread_join了
可分离线程的使用场景
1、主线程不需要等待子线程
2、主线程不关心子线程的返回码
可以通过pthread_detach(thread1)将线程thread1进行分离
一般会在线程初始化进行系统属性设置线程分离的状态
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
void* fun1(void* arg)
{
int* p = malloc(sizeof(int));
*p = 111;
return p;
}
void* fun2(void* arg)
{
while(1)
{
printf("living...\n");
}
}
int main(int arg,char* args[])
{
pthread_t thread1,thread2;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if(pthread_create(&thread1,&attr,fun1,NULL) != 0)
{
printf("thread1 failure\n");
}
if(pthread_create(&thread2,&attr,fun2,NULL) != 0)
{
printf("thread2 failure\n");
}
pthread_attr_destroy(&attr);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
printf("main end\n");
return EXIT_SUCCESS;
}
~
在默认设置属性的方法中设置线程是否分离
同时一个线程可以控制另一个线程的退出通过pthread_cancel()进行
#include <pthread.h>
#include <errno.h>
void* fun1(void* arg)
{
while(1)
{
printf("living...\n");
}
return NULL;
}
void* fun2(void* arg)
{
sleep(5);
pthread_t thr;
thr =*((pthread_t*)arg);
pthread_cancel(thr);
return NULL;
}
int main(int arg,char* args[])
{
pthread_t thread1,thread2;
int id1 = 1;
int id2 = 2;
if( pthread_create(&thread1,NULL,fun1,&id1) != 0 )
{
printf("%s\n",strerror(errno));
}
if(pthread_create(&thread2,NULL,fun2,&thread1)!= 0)
{
printf("fali\n");
}
//int* p = NULL;
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
//pthread_detach(thread1);
//printf("return value:%d\n",*p);
return EXIT_SUCCESS;
}
thread2就终止了thread1的线程 这个函数会立刻结束线程
线程同步的技术就是加锁与解锁的过程 就直接看代码吧:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* fun2(void* arg)
{
pthread_mutex_lock(&mutex);
int i = 0;
int pid =*((int*)arg);
for(i =0 ;i < 10;i++)
{
printf("thread%d living...\n",pid);
sleep(1);
}
pthread_mutex_unlock(&mutex);
printf("thread%d died\n",pid);
}
int main(int arg,char* args[])
{
pthread_t thread1,thread2;
int id1 = 1;
int id2 = 2;
if(pthread_create(&thread1,NULL,fun2,&id1) != 0)
{
printf("thread1 failure\n");
}
if(pthread_create(&thread2,NULL,fun2,&id2) != 0)
{
printf("thread2 failure\n");
}
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
printf("main end\n");
return EXIT_SUCCESS
创建
pthread_mutex_t锁 然后进行加锁和解锁的过程
OK 线程的基本学习就到此啦!!