原文地址:linux下C语言线程的介绍作者:Ree雪影
今天一天小记。。
linux下线程的介绍
下面是主要的函数介绍:
创建线程要实现的函数
int pthread_create(pthread_t *thread,pthread_attr_t *attr,void * (*start_routine)(void *),void *arg);
gcc编译多线程程序时,必须与pthread函数库连接。有2种方法
1.gcc -D_REENTRANT -o aa 1.c -lpthread
2.gcc -pthread -o aa 1.c
用信号量进行同步
1.信号量的创建
int sem_init(sem_t *sem,int pshared,unsigned int value);
2.控制信号量
int sem_wait(sem_t *sem);
int sem_post(sem_t *sem);
3.清理信号
int sem_destory(sem_t sem);
用互斥量进行同步
1.线程的互斥量
pthread_mutex_t a_mutex = PTHREAD_mutex_INITIALiZER或者PTHREAD_RECURSIVE_mutex_INITIALIZER_NP(同一个线程多次锁定它不会产生死锁);
int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutexattr_t *mutexattr);
2.锁定和解除锁定互斥量
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
3.销毁互斥量
int int pthread_mutex_destroy(pthread_mutex_t *mutex);
线程属性对象
1.创建线程属性对象
int pthread_attr_init(pthread_attr_t *attr);
2.回收函数(顾名思义)
int pthread_attr_destroy(pthread_attr_t *attr);
3.设置不同的线程属性
int pthread_attr_setdetachstate(pthread_attr_t *attr,int detachstate);(设置脱离状态)
int pthread_attr_setschedpolicy(pthread_attr_t *attr,int policy);(改变调度属性)
...
可用于创建脱离线程。
取消一个线程
1.线程终止
int pthread_cancel(pthread_t thread);
2.设置线程的取消状态
int pthread_setcancelstate(int state,int *oldstate);
3.设置取消类型
int pthread_setcanceltype(int type,int *oldtype);
用条件变量改进线程同步
1.创建和初始化条件变量
pthread_cond_t got_request = PTHREAD_COND_INITIALIZER;
2.向条件变量发出信号
int rc = pthread_cond_singal(&got_request);
或使用广播函数
int rc = pthread_cond_broadcast(&got_request);
4.守候条件变量
pthread_cond_wait()
pthread_cond_timedwait()
下面是一些例子程序的源代码:
1.
#include <stdio.h>
#include <pthread.h>
//新线程将要执行的函数
void * do_loop(void * data)
{
int i;int j;
int me = *((int *)data); //线程编号
for(i=0;i<10;i++)
{
for(j=0;j<500000;j++) //延迟循环
;
printf("'%d' - Got '%d'n",me,i);
}
//退出线程
pthread_exit(NULL);
}
//main函数
int main(int argc,char *argv[])
{
int ret; //线程创建函数的返回值
pthread_t p_thread; //线程的ID号
int a = 1; //子线程编号
int b = 2; //主线程编号
//创建一个子线程
ret = pthread_create(&p_thread,NULL,do_loop,(void *)&a);
//在主线程中也执行do_loop
do_loop((void *)&b);
//不会执行到这里
return 0;
}
2.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
char message[] = "Hello world!";
void *thread_function(void *arg) {
printf("thread_function is running. Argument was %sn",(char *)arg);
sleep(3);
strcpy(message,"Bye!");
pthread_exit("Thank you for the CPU time!");
}
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("Thread creation failed!");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...n");
res = pthread_join(a_thread,&thread_result);
if(res!=0) {
perror("Thread join failed!");
exit(EXIT_FAILURE);
}
printf("Thread joined,it return %sn",(char *)thread_result);
printf("Message is now %sn",message);
exit(EXIT_SUCCESS);
}
3.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
sem_t bin_sem;
#define WORK_SIZE 1024
char work_area[WORK_SIZE];
void *thread_function(void *arg) {
sem_wait(&bin_sem);
while(strncmp("end",work_area,3)!=0) {
printf("You input %d charactersn",strlen(work_area)-1);
sem_wait(&bin_sem);
}
pthread_exit(NULL);
}
int main() {
int res;
pthread_t a_thread;
void *thread_result;
res = sem_init(&bin_sem,0,0);
if(res!=0) {
perror("Semaphore creation failed!");
exit(EXIT_FAILURE);
}
res = pthread_create(&a_thread,NULL,thread_function,NULL);
if(res!=0) {
perror("Thread creation failed!");
exit(EXIT_FAILURE);
}
printf("Input some text.Enter 'end' to finish:n");
while(strncmp("end",work_area,3)!=0) {
fgets(work_area,WORK_SIZE,stdin);
sem_post(&bin_sem);
}
printf("nWaiting for thread to finish...n");
res = pthread_join(a_thread,&thread_result);
if(res!=0) {
perror("Thread join failed!");
exit(EXIT_FAILURE);
}
printf("Thread joinedn");
sem_destroy(&bin_sem);
exit(EXIT_SUCCESS);
}
4.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 6
void *thread_function(void *arg) {
int my_number = (int) arg;
int rand_num;
printf("thread_function is running.Argument was %dn",my_number);
rand_num = 1 + (int)(9.0*rand()/(RAND_MAX+1.0));
sleep(rand_num);
printf("Bye from %dn",my_number);
pthread_exit(NULL);
}
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 < NUM_THREADS;lots_of_threads++) {
res = pthread_create(&(a_thread[lots_of_threads]),NULL,thread_function,(void *)lots_of_threads);
if(res!=0) {
perror("Thread creation failed!");
exit(EXIT_FAILURE);
}
sleep(1);
}
printf("Waiting for threads to finish...n");
for(lots_of_threads = NUM_THREADS - 1;lots_of_threads >= 0;lots_of_threads--) {
res = pthread_join(a_thread[lots_of_threads],&thread_result);
if(res == 0) {
printf("Picked up a thread!n");
}
else {
perror("failed!");
}
}
printf("ALL DONE!");
exit(EXIT_SUCCESS);
}
linux下线程的介绍
下面是主要的函数介绍:
创建线程要实现的函数
gcc编译多线程程序时,必须与pthread函数库连接。有2种方法
用信号量进行同步
用互斥量进行同步
线程属性对象
取消一个线程
用条件变量改进线程同步
下面是一些例子程序的源代码:
1.
#include <stdio.h>
#include <pthread.h>
//新线程将要执行的函数
void * do_loop(void * data)
{
}
//main函数
int main(int argc,char *argv[])
{
}
2.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
char message[] = "Hello world!";
void *thread_function(void *arg) {
}
int main()
{
}
3.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
sem_t bin_sem;
#define WORK_SIZE 1024
char work_area[WORK_SIZE];
void *thread_function(void *arg) {
}
int main() {
}
4.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 6
void *thread_function(void *arg) {
}
int main() {
}