如何一次创建多个线程:

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

#define MAX_THREAD_NUM 10
void* thread_fun(void*arg)
{
    int i = *(int*)arg;
    printf("[%d]thread start up.\n",i);
    pthread_exit(0);
}


int main()
{
    pthread_t tid[MAX_THREAD_NUM];
    for(int i=0;i<MAX_THREAD_NUM;++i)
    {
        pthread_create(&tid[i],NULL,thread_fun,&i);
        sleep(1);
    } 
    for(int i=0;i<MAX_THREAD_NUM;++i)
    {
        pthread_join(tid[i],NULL);
    }
    return 0;

}
Linux下的多线程编程
运行结果

上述程序用for 循环创建了多个线程,并将参数传递给线程。sleep(1)函数一定要加上,不然得不到想要的效果。sleep()函数的等待并不释放cpu资源。

多线程的同步之互斥量:

#include<stdio.h>
#include<unistd.h>
#include<malloc.h>
#include<pthread.h>
//pthread_mutex_t mutex =  PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex;
int count = 0;
void* thread_fun1(void* arg)
{
    pthread_mutex_lock(&mutex);
    count = 10;
    sleep(3);
    printf("This is thread fun1.count = %d\n",count);
    pthread_mutex_unlock(&mutex);
}
void* thread_fun2(void* arg)
{
    pthread_mutex_lock(&mutex);
    count = 100;
    printf("This is thread fun2.count = %d\n",count);
    pthread_mutex_unlock(&mutex);
}

int main()
{
    pthread_mutex_init(&mutex, NULL);
    pthread_t tid1,tid2;
    pthread_create(&tid1,NULL,thread_fun1,NULL);
    sleep(1);
    pthread_create(&tid2,NULL,thread_fun2,NULL);
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    pthread_mutex_destroy(&mutex);
    return 0;
}
Linux下的多线程编程
运行结果

相应的函数都应该熟练掌握。

trylock()函数:

#include<stdio.h>
#include<unistd.h>
#include<malloc.h>
#include<pthread.h>
//pthread_mutex_t mutex =  PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex;
int count = 0;
void* thread_fun1(void* arg)
{
    pthread_mutex_lock(&mutex);
    count = 10;
   // sleep(3);
    printf("This is thread fun1.count = %d\n",count);
    pthread_mutex_unlock(&mutex);
}
void* thread_fun2(void* arg)
{
    sleep(1);
    int ret = pthread_mutex_trylock(&mutex);
    if(ret == 0)
    { 
        printf("Try lock mutex success.\n");
        count = 100;
        printf("This is thread fun2.count = %d\n",count);
        pthread_mutex_unlock(&mutex);
    }
    else
        printf("Try lock mutex fail. ret = %d\n",ret);
}

int main()
{
    pthread_mutex_init(&mutex, NULL);
    pthread_t tid1,tid2;
    pthread_create(&tid1,NULL,thread_fun1,NULL);
    sleep(1);
    pthread_create(&tid2,NULL,thread_fun2,NULL);
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    pthread_mutex_destroy(&mutex);
    return 0;
}
Linux下的多线程编程
运行结果

如果注释掉thread_fun1()函数中的pthread_mutex_unlock(&mutex),则会打印“Try lock mutex fail.”并返回打开失败的错误类型。

多进程同步之条件量:

#include<stdio.h>
#include<unistd.h>
#include<malloc.h>
#include<pthread.h>
//pthread_mutex_t mutex =  PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int count = 0;
void* thread_fun1(void* arg)
{
    pthread_mutex_lock(&mutex);
    count = 10;
    //sleep(3);
    printf("aaaaaaaaaaaaa.\n");
    pthread_cond_wait(&cond, &mutex);//pthread_mutex_unclock().
    printf("bbbbbbbbbbbbb.\n");
    printf("This is thread fun1.count = %d\n",count);
    //pthread_mutex_unlock(&mutex);
}
void* thread_fun2(void* arg)
{
    sleep(1);
    int ret = pthread_mutex_trylock(&mutex);
    if(ret == 0)
    {
        printf("Try lock mutex success.\n");
        count = 100;
        printf("This is thread fun2.count = %d\n",count);
        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&cond);
    }
    else
        printf("Try lock mutex fail.ret = %d\n",ret);
}

int main()
{
    pthread_mutex_init(&mutex, NULL);
    pthread_t tid1,tid2;
    pthread_create(&tid1,NULL,thread_fun1,NULL);
    sleep(1);
    pthread_create(&tid2,NULL,thread_fun2,NULL);
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    pthread_mutex_destroy(&mutex);
    return 0;
}
Linux下的多线程编程
运行结果

学会pthread_cond_wait()函数和pthread_cond_signal()函数的用法,理解这一对函数是怎么实现线程之间的同步的。

下面的场景,你将体会到pthread_cond_broadcast()函数的作用:

#include<iostream>
#include<unistd.h>
#include<stdio.h>
#include<pthread.h>
#define MAX_SIZE 5
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* pthread_fun(void* arg)
{
    int i = *(int *)arg;
    printf("[%d]thread start up.\n",i);
    pthread_mutex_lock(&mutex);
    printf("[%d]thread wait......\n",i);
    pthread_cond_wait(&cond,&mutex);
    printf("[%d] thread wake up.\n",i);
    pthread_mutex_unlock(&mutex);
}

int main()
{
    pthread_t tid[MAX_SIZE];
    for(int i=0;i<MAX_SIZE;++i)
    {
        pthread_create(&tid[i], NULL, pthread_fun, &i);
        sleep(1);
    }
    sleep(3);
    pthread_cond_broadcast(&cond);//唤醒多个阻塞的线程
    for(int i=0;i<MAX_SIZE;++i)
    {
        pthread_join(tid[i], NULL);
    }
    return 0;
}
Linux下的多线程编程
运行的结果

条件量的一个简单的应用:用两个线程轮流交换数完1-10,一个进程数奇数,一个进程数偶数。

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#define MAX_COUNT 10
static int count = 1;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t is_odd = PTHREAD_COND_INITIALIZER;
pthread_cond_t is_even = PTHREAD_COND_INITIALIZER;
void* A_fun(void *arg)
{
    pthread_mutex_lock(&mutex);
    while(count <= MAX_COUNT)
    {
        if(count % 2 == 1)
        {
            printf("In A fun : %d\n", count++);
            pthread_cond_signal(&is_even);
        }
        else
        {
            pthread_cond_wait(&is_odd, &mutex);
        }
    }
    pthread_mutex_unlock(&mutex);
}


void* B_fun(void *arg)
{
    pthread_mutex_lock(&mutex);
    while(count <= MAX_COUNT)
    {
        if(count % 2 == 0)
        {
            printf("In B fun : %d\n", count++);
            pthread_cond_signal(&is_odd);
        }
        else
        {
            pthread_cond_wait(&is_even, &mutex);
        }
    }

    pthread_mutex_unlock(&mutex);
}


int main()
{
    pthread_t tid1, tid2;
    pthread_create(&tid2, NULL, B_fun, NULL);
    sleep(5);
    pthread_create(&tid1, NULL, A_fun, NULL);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
}
Linux下的多线程编程
运行的结果

从代码可以看出,线程2先执行,过了5秒之后,线程1 才执行。程序执行的结果与两个进程的先后没有关系,符合编写程序的本意。

相关文章: