线程笔记之示例

1、线程的创建

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

void Mythread1()
{ 
    int i;
    
    for(i = 0;i < 100;i++)
    {
        printf("The thread1 is create!\n");
        sleep(1);
    }
}

void Mythread2()
{ 
    int i;
    
    for(i = 0;i < 100;i++)
    {
        printf("The thread2 is create!\n");
        sleep(1);
    }
}

int main()
{
    int ret;
    pthread_t id1;
    pthread_t id2;

    ret = pthread_create(&id1,NULL,(void *)Mythread1,NULL);
    
    if(ret < 0)
    {
        printf("Create pthread failed!\n");
        return 1;
    }
    
    ret = pthread_create(&id2,NULL,(void *)Mythread2,NULL);
    
    if(ret < 0)
    {
        printf("Create pthread failed!\n");
        return 1;
    }

    pthread_join(id1,NULL);
    pthread_join(id2,NULL);
    return 0;
}



调试结果:

The thread1 is create!
The thread2 is create!
The thread1 is create!
The thread2 is create!
The thread1 is create!
The thread2 is create!
The thread1 is create!
The thread2 is create!


2、线程进行参数传递,字符串传递:

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

void *pass(void *arg)
{
    char ptr[10] ={0};

    strcpy(ptr,arg);
    printf("pass %s is done!\n",ptr);

    return NULL;
}

int main()
{
    pthread_t id;
    int ret;
    char str[10] = {0};

    printf("Please input the string:\n");
    scanf("%s",str);

    ret = pthread_create(&id,NULL,pass,str);

    if(ret < 0)
    {
        printf("Create pthread failed!\n");
        return 1;
    }

    sleep(1);
    printf("Pthread is created...\n");

    pthread_join(id,NULL);
    return 0;
}


调试结果:

Please input the string:
hello
pass hello is done!
Pthread is created...


3、线程传递参数,传递数据:

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

void *Pass(void *arg)
{
    int *num;
    num = (int *) arg;

    printf("Num is passed!Num is %d\n",*num);
    return NULL;
}

int main()
{
    pthread_t id;
    int ret;

    int pass;
    void *attr = &pass;

    printf("Please input the pass num:\n");
    scanf("%d",&pass);

    ret = pthread_create(&id,NULL,Pass,attr);

    if(ret < 0)
    {
        printf("Create pthread failed!\n");
        return 1;
    }

    sleep(1);
    printf("pthread is created...\n");
    return 0;
}

调试结果:

Please input the pass num:
1
Num is passed!Num is 1
pthread is created...

4、线程参数传递,传递数据结构:

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

struct test
{
    int num;
    char *str;
};

void *pass(void *arg)
{
    struct test *tmp;
    tmp = (struct test *)arg;

    printf("Struct is passed!\nThe passsed num is %d,the passed char is %s\n",tmp->num,tmp->str);

    return NULL;
}

int main()
{
    pthread_t tidp;
    int ret;
    struct test *T1;

    T1 = (struct test*)malloc(sizeof(struct test)); 
    T1->num = 3;
    T1->str = "daw";

    ret = pthread_create(&tidp,NULL,pass,(void *)T1);

    if(ret < 0)
    {
        printf("Create pthread failed!\n");
        return 1;
    }

    sleep(1);
    printf("Pthread is created...\n");
    
    pthread_join(tidp,NULL);
    return 0;
}

调试结果:

Struct is passed!
The passsed num is 3,the passed char is daw
Pthread is created...


5、线程的互斥锁:

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

pthread_mutex_t mutex;

void *fun1(void *arg)
{
    int val;
    val = pthread_mutex_lock(&mutex);

    if(val != 0)
    {
        printf("Lock error!\n");
        return 1;
    }

    printf("This fun1!\n");
    pthread_mutex_unlock(&mutex);

    return NULL;
}

void *fun2(void *arg)
{
    int val;
    val = pthread_mutex_lock(&mutex);

    if(val != 0)
    {
        printf("Lock error!\n");
        return 1;
    }

    printf("This fun2!\n");
    pthread_mutex_unlock(&mutex);
    
    return NULL;
}

int main()
{
    pthread_t id1;
    pthread_t id2;

    if(pthread_mutex_init(&mutex,NULL) != 0)
    {
        printf("Init mutex failed!\n");
        return 1;
    }

    if(pthread_create(&id1,NULL,fun1,NULL) < 0)
    {
        printf("Create pthread1 failed!\n");
        return 1;
    }

    if(pthread_create(&id2,NULL,fun2,NULL) < 0)
    {
        printf("Create pthread2 failed!\n");
        return 1;
    }

    sleep(1);
    printf("Pthread is created!\n");
    pthread_join(id1,NULL);
    pthread_join(id2,NULL);

    pthread_mutex_destroy(&mutex);
    return 0;
}


调试结果:

This fun1!
This fun2!
Pthread is created!





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值