想要了解线程的控制,首先我们要理解线程的概念。
线程的概念:
进程拥有独立的内存和系统资源,而在一个进程内部,线程之间的资源是共享的,系统不会为线程分配系统资源。
进程拥有系统资源,在系统切换的时候,操作系统要保留进程占用的资源;线程的切换不需要保留系统资源,切换效率远高于进程。
线程有程序运行的入口地址,但是线程不能独立运行。由于线程不占有系统资源,所以线程必须放在进程中。进程可以被操作系统直接调度。一个进程内部的多个线程可共享资源和调度,不同进程之间的线程资源不能直接共享。
#include <pthread.h>
int pthread_create(pthread_t *thread, constpthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
#include <pthread.h>
pthread_tpthread_self(void);
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
#include <pthread.h>
void pthread_exit(void *retval);
int pthread_cancel(pthread_t thread);
#include
#include
void thread(void)
{
int i;
for (i=0;i<3;i++)
printf("This is a pthread.\n");
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *) thread,NULL);
if (ret!=0)
{
printf ("Create pthread error!\n");
exit (1);
}
for (i=0;i<3;i++)
printf("This is the main process.\n");
pthread_join(id,NULL);
return(0);
}
编译并运行,因线程相关函数是运行在用户空间的线程库pthread.h实现,所以编译的时候要加上-lpthread选项。
intsem_init(sem_t*sem,intpshared, unsignedint value);
其中,第一个参数是sem_t结构的指针,该结构用于保存信号量的信息。第二个参数控制信号量的类型,如果参数值为0,表示该信号量是局部的,否则其它程序就能共享这个信号量。第三个参数是信号量的初始值。
int pthread_mutex_init(pthread_mutex_t*mutex, const pthread_mutexattr_t*mutexattr);
int pthread_mutex_lock(pthread_mutex_t*mutex));
int pthread_mutex_unlock(pthread_mutex_t*mutex);
int pthread_mutex_destroy(pthread_mutex_t*mutex);
pthread_mutex_init( )函数用于创建一个互斥量,第一个参数是指向互斥量的数据结构pthread_mutex_t的指针,第二个参数是定义互斥量属性的pthread_mutexattrt结构的指针,它的默认类型是fast。类似于信号量的使用方法。
pthread_mutex_lock( )是对互斥量进行锁定操作,pthread_mutex_unlock()是对互斥量进行解锁操作。函数pthread_mutex_destroy()的作用是清除互斥量。
1. mutex要由获得锁的线程来释放(谁获得,谁释放)。而semaphore可以由其它线程释放。
2.初始状态可能不一样:mutex的初始值是1 ,而semaphore的初始值可能是0(或者为1)。
intpthread_cancel(pthread_tthread);
参数中指定的线程在收到取消请求后,会对自己稍做一些处理,然后结束。
在线程函数中可使用pthread_setcancelstate()设置自己的取消状态,该函数的一般形式是:
intpthread_setcanceltype(int type,int*oldtype);
#include
#include
#include
#include //包含线程库
#include
void *thread_function(void *arg); //定义线程函数原型
char message[]= "THREAD_TEST"; //定义公用的内存空间
int main()
{
int res; //用于保存线程的返回值
pthread_t a_thread; //用于保存线程的标识符等信息
void *thread_result; //用于接受线程结束时的返回值
res=pthread_create(&a_thread,NULL,thread_function,(void*)message); //创建线程
if(res!=0) //判断线程是否有错误
{
perror("线程创建失败");
exit(EXIT_FAILURE);
}
printf("等待线程结束...\n");
res=pthread_join(a_thread, &thread_result); //等待线程结束
if(res!=0) //判断线程是否有错误
{
perror("等待线程结束");
exit(EXIT_FAILURE);
}
printf("线程已结束,返回值:%s\n",(char *)thread_result); //输出线程返回的消息
printf("Message的值为:%s\n",message); //输出公用的内存空间的值
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg) //定义线程函数的细节
{
printf("线程在运行,参数为:%s\n",(char *)arg); //输出线程的参数
sleep(3); //使线程休眠3秒
strcpy(message,"线程修改"); //修改公用的内存空间的值
pthread_exit("线程执行完毕"); //结束线程
}
#include
#include
#include
#include
#define NUM_THREADS 6 //定义线程的总数
void *thread_function(void *arg);
int main()
{
int res;
pthread_t a_thread[NUM_THREADS]; //定义线程数组
void *thread_result;
int lots_of_threads;
for(lots_of_threads=0;lots_of_threads=0;lots_of_threads--)
{
res=pthread_join(a_thread[lots_of_threads],&thread_result); //等待线程结束
if(res==0)
{
printf("结束一个线程\n");
}
else
{
perror("线程结束失败");
}
}
printf("线程结束失败\n");
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg) //定义线程函数
{
int my_number=*(int *)arg; //接受主线程传递的参数,该参数可以是任意类型
int rand_num;
printf("线程函数已运行,参数为:%d\n",my_number);
rand_num=1+(int)(9.0*rand()/(RAND_MAX+1.0)); //获得一个随机数
sleep(rand_num); //线程以随机数定义的时间休眠
printf("第%d个线程结束\n",my_number); //结束线程
pthread_exit(NULL);
}