Linux多线程

43 篇文章 0 订阅
22 篇文章 2 订阅

创建线程
#include <pthread.h>

int pthread_create(pthread_t *tidp,const pthread_attr_t *attr,void *(*start_rtn)(void),void *arg)
tidp:线程id
attr:线程属性(通常为空)
start_rtn:线程要执行的函数
arg:start_rtn的参数

编译的时候需要加上pthread的库
gcc test.c -lpthread -o test

代码:
创建线程

#include <stdio.h>
#include <pthread.h>

void *myThread1(void)
{
    int i;
    for (i=0; i<3; i++)
    {
        printf("This is the 1st pthread,created by zieckey.\n");
        sleep(1);//Let this thread to sleep 1 second,and then continue to run
    }
}

void *myThread2(void)
{
    int i;
    for (i=0; i<3; i++)
    {
        printf("This is the 2st pthread,created by zieckey.\n");
        sleep(1);
    }
}

int main()
{
    int i=0, ret=0;
    pthread_t id1,id2;
    
    /*创建线程1*/
    ret = pthread_create(&id1, NULL, (void*)myThread1, NULL);
    if (ret)
    {
        printf("Create pthread error!\n");
        return 1;
    }
    
    /*创建线程2*/
    ret = pthread_create(&id2, NULL, (void*)myThread2, NULL);
    if (ret)
    {
        printf("Create pthread error!\n");
        return 1;
    }
    
    pthread_join(id1, NULL);
    pthread_join(id2, NULL);
    
    return 0;
}

向线程函数传递一个整型的参数

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *create(void *arg)
{
    int *num;
    num=(int *)arg;
    printf("create parameter is %d \n",*num);
    return (void *)0;
}

int main(int argc ,char *argv[])
{
    pthread_t tidp;
    int error;
    
    int test=4;
    int *attr=&test;  //把变量传递给指针 
    
    error=pthread_create(&tidp,NULL,create,(void *)attr);

    if(error)
        {
        printf("pthread_create is created is not created ... \n");
        return -1;
        }
    sleep(1);
    printf("pthread_create is created ...\n");
    return 0;        
}

有个share代码

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

int a = 1;

void *create(void *arg)
{
    printf("new pthread ... \n");
    printf("a=%d  \n",a);
    return (void *)0;
}

int main(int argc,char *argv[])
{
    pthread_t tidp;
    int error;
    
    int a=5;

     printf("a = %d\n",a);
    
    error=pthread_create(&tidp, NULL, create, NULL);

    if(error!=0)
    {
        printf("new thread is not create ... \n");
        return -1;
    }
    
    sleep(1);
    
    printf("new thread is created ... \n");
    return 0;
}

观察两次打印出来的a的值。第一次打印出a的值是5(栈),第二次是线程里的a值(数据段的数据),也是全局变量的值,应该是1。
当有全局变量和局部变量名相同时,首先应该调用局部变量的值。局部变量个全局变量都储存在栈中。

终止线程
如果进程中任何一个线程中调用exit或_exit,那么整个进程都会终止。线程的正常退出方式有:

  1. 线程从启动例程中返回
  2. 线程可以被另一个进程终止
  3. 线程自己调用pthread_exit函数

线程退出
#include<pthread.h>
void pthread_exit(void* rval_ptr)
功能:终止调用线程
Rval_ptr:线程退出返回值的指针

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *create(void *arg)
{
    printf("new thread is created ... \n");
    return (void *)8;
}

int main(int argc,char *argv[])
{
    pthread_t tid;
    int error;
    void *temp;

    error = pthread_create(&tid, NULL, create, NULL);
    printf("main thread!\n");

    if( error )
    {
        printf("thread is not created ... \n");
        return -1;
    }
    error = pthread_join(tid, &temp);

    if( error )
    {
        printf("thread is not exit ... \n");
        return -2;
    }
    
    printf("thread is exit code %d \n", (int )temp);
    return 0;
}

使用return 来终止线程程序

等待
#include <pthread.h>
int pthread_join(pthread_t tid,void **rval_ptr)
功能:阻塞调用线程,直到指定的线程终止
tid:等待退出的线程id
rval_ptr:线程退出的返回值的指针

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>

void *thread(void *str)
{
    int i;
    for (i = 0; i < 10; ++i)
    {
        sleep(2);
        printf( "This in the thread : %d\n" , i );
    }
    return NULL;
}

int main()
{
    pthread_t pth;
    int i;
    int ret = pthread_create(&pth, NULL, thread, (void *)(i));
    
    pthread_join(pth, NULL);
    printf("123\n");
    for (i = 0; i < 10; ++i)
    {
        //sleep(1);
        printf( "This in the main : %d\n" , i );
    }
    
    return 0;
}

清除
线程终止有两种情况:正常终止和非正常终止。线程主动调用pthread_exit或者从线程函数中return都将使线程正常退出,这是可预见的退出方式;非正常终止是线程在其他线程的干预下,或者由于自身运行出错(比如访问非法地址)而退出,这种退出方式是不可预见的。
但是这两种终止都存在资源释放问题,如何保证线程终止时能顺利释放掉自己所占用的资源,是一个必须考虑解决的问题。

从pthread_cleanup_push的调用点到pthread_cleanup_pop之间的程序段中的终止动作(包括调用pthread_exit()和异常终止,不包括return)都将执行pthread_cleanup_push()所指定的清理函数。
#include <pthread.h>
void pthread_cleanup_push(void (*rtn)(void *),void *arg)
功能:将清除函数压入清除栈
rtn:清除函数
arg:清除函数的参数
void pthread_cleanup_pop(int execute)
将清除函数弹出清除栈
参数:execute执行到pthread_cleanup_pop()时是否在弹出清理函数的同时执行该函数,非0:执行;0:不执行

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *clean(void *arg)
{
    printf("cleanup :%s  \n",(char *)arg);
    return (void *)0;
}
void *thr_fn1(void *arg)
{
    printf("thread 1 start  \n");
    pthread_cleanup_push( (void*)clean,"thread 1 first handler");
    pthread_cleanup_push( (void*)clean,"thread 1 second hadler");
    printf("thread 1 push complete  \n");
    if(arg)
    {
        return((void *)1);
    }
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    return (void *)1;
}


void *thr_fn2(void *arg)
{
    printf("thread 2 start  \n");
    pthread_cleanup_push( (void*)clean,"thread 2 first handler");
    pthread_cleanup_push( (void*)clean,"thread 2 second handler");
    printf("thread 2 push complete  \n");
    if(arg)
    {
        pthread_exit((void *)2);
    }
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    pthread_exit((void *)2);
}



int main(void)
{
    int err;
    pthread_t tid1,tid2;
    void *tret;

    err=pthread_create(&tid1,NULL,thr_fn1,(void *)1);
    if(err!=0)
    {
        printf("error .... \n");
        return -1;
    }
    err=pthread_create(&tid2,NULL,thr_fn2,(void *)1);

    if(err!=0)
    {
        printf("error .... \n");
        return -1;
    }
    err=pthread_join(tid1,&tret);
    if(err!=0)
    {
        printf("error .... \n");
        return -1;
    }
    printf("thread 1 exit code %d  \n",(int)tret);

    err=pthread_join(tid2,&tret);
    if(err!=0)
    {
        printf("error .... ");
        return -1;
    }

    printf("thread 2 exit code %d  \n",(int)tret);
    
    return 1;
}

线程1和线程2分别打印出什么

thread 1 start
thread 1 push complete
因为线程1有return终止,将不执行push与pop之间的清除函数

thread 2 start
thread 2 push complete
cleanup :thread 2 second handler (后压入栈中,先弹出)
cleanup :thread 2 first handler

这篇文章写得很好:
https://cloud.tencent.com/developer/article/1389883

下面是我写一个创建多线程的例子,使用crtl+c可以顺序退出线程
在linux中输入: g++ test.cpp -lpthread -o test 编译

#include <stdio.h>
#include <pthread.h>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdexcept>
#include <signal.h>
#include <malloc.h>
using namespace std;


int num=0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int flag = -1;

int GetInput()
{
   fd_set rfds;
   struct  timeval tv;
   int c = 0;
   FD_ZERO(&rfds);
   FD_SET(0,&rfds);
   tv.tv_sec = 1;
   tv.tv_usec = 1;
   //检测键盘是否有输入
   if(select(1,&rfds,NULL,NULL,&tv)>0){
	c=getchar(); 
    } 
    return c;  
}
int num_max=0;
void *thread(void *arg){
      int i;
     static int x=0;
      i =*(int*)arg;
      while(1){
	if(flag==1){
	   if(i==x){
	     x++;
	     break;
	   }
	}
        sleep(1);
	pthread_mutex_lock(&mutex);
	num++;
	printf("my Thread%d:num=%d,pid=%ld \n",i,num,pthread_self());
	pthread_mutex_unlock(&mutex);
 	}
    printf("Thread%d exit\n",i);
    return (void*)0;

}
void sighandler(int signum){
	int sig = signum;
 	flag=1;
        
}
int main(int argc,char *argv[]){

    num_max = atoi(argv[1]);
	int ret=0;
    pthread_t *id = (pthread_t *)malloc(sizeof(pthread_t)*num_max);
	int *pt = (int*)malloc(sizeof(int)*num_max);
 	for(int i=0;i<num_max;i++){
        pt[i]=i;
		ret = pthread_create(&id[i],NULL,thread,&pt[i]);
		if(ret){
		   printf("Create pthread:%d error!\n",i);
		   return -1;		
		}	
	}
    signal(SIGINT, sighandler);
    for(int i=0;i<num_max;i++){
	pthread_join(id[i],NULL);
    }
	pthread_mutex_destroy(&mutex);
    free(id);
	free(pt);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值