linux 多线程

来源:互联网 发布:居住证 知乎 编辑:程序博客网 时间:2024/06/11 14:44

一个简单的多线程程序:

#include <stdio.h>#include <unistd.h>#include <pthread.h>static int run = 1;static int retvalue;void *thread(void *threadid){    int *tid = threadid;    printf("sub_thread init done, input:%d\n", *tid);    while (*tid)    {        printf("sub_thread is running\n");        usleep(1);    }    printf("sub_thread quit\n");    retvalue = 8;    pthread_exit((void*)&retvalue);}int main(void){    pthread_t id;    int ret = -1;    int times = 3;    int i = 0;    int *ret_join = NULL;    ret = pthread_create(&id, NULL, (void*)thread, &run);    if (0 != ret)    {        printf("create thread failed\n");        return 1;    }    usleep(1);    for (;i < times; i++)    {        printf("main thread print\n");        usleep(1);    }    run = 0;    pthread_join(id, (void*)&ret_join);    printf("thread return:%d\n", *ret_join);return 0;}
编译需要链接线程库:#gcc -o test pthread.c -lpthread

1、int ret = pthread_create(&id, NULL, (void*)thread, &run); 线程创建函数。

函数原型:int pthread_create(pthread_t *tidp,const pthread_attr_t *attr,(void*)(*start_rtn)(void*),void *arg);

attr 第二个参数用来设置线程属性。

第三个参数是线程运行函数的起始地址。

arg 最后一个参数是运行函数的参数,传入参数。所有的参数都要按照格式填写。

tidp 第一个参数为指向线程标识符的指针。


2、pthread_exit((void*)&retvalue);   线程通过调用pthread_exit函数终止执行,就如同进程在结束时调用exit函数一样。这个函数的作用是,终止调用它的线程并返回一个指向某个对象的指针。传出值指针可以被 pthread_join 捕获。


3、pthread_join(id, (void*)&ret_join);   函数pthread_join用来等待一个线程的结束。阻塞的意思是 主线程会在调用pthread_join()处等待,直到子线程消亡了才会继续往下执行。可以通过不给run置零,然后去掉pthread_join函数来测试线程运行情况。

pthread_detach使子线程进入分离状态的意思是 主线程不能再用pthread_join()和子线程同步,就是说 对子线程调用 pthread_detach之后,主线程再调用pthread_join()不会导致等待主线程阻塞等待,这时候主线程和子线程相当于两个执行序列,两者之间处于一种 分离 状态。

函数原型: int pthread_join(pthread_t thread, void **retval);


通过信号同步来编写多线程程序,简单程序如下:

#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <semaphore.h>#define SIZE 1024void *thread_function(void *arg);char buffer[SIZE];sem_t sem;int main(){    int res;    pthread_t a_thread;    void *thread_result;    res = sem_init(&sem, 0, 0);    if (res != 0)    {        perror("Sem init failed");        exit(EXIT_FAILURE);    }    res = pthread_create(&a_thread, NULL, thread_function, NULL);    if (res != 0)    {        perror("Thread create failed");        exit(EXIT_FAILURE);    }    printf("Input some text. Enter 'end' to finish\n");    while (scanf("%s", buffer))    {        sem_post(&sem);        if (strncmp("end", buffer, 3) == 0)            break;    }    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 join\n");    sem_destroy(&sem);    exit(EXIT_SUCCESS);}void *thread_function(void *arg){    sem_wait(&sem);    while (strncmp("end", buffer, 3) != 0)    {        printf("You input %d characters\n", strlen(buffer));        sem_wait(&sem);    }    pthread_exit(NULL);}
编译需要链接线程库:#gcc -o test pthread.c -lpthread


sem_post   的作用是以原子操作的方式给信号量的值加1

sem_wait   的作用是以原子操作的方式给信号量的值减1,但它会等到信号量非0时才会开始减法操作。如果对值为0的信号量调用sem_wait,这个函数就会等待,直到有线程增加了该信号量的值使其不再为0

sem_trywait()和sem_wait()有一点不同,即如果信号量的当前值为0,则返回错误而不是阻塞调用。错误值errno设置为EAGAIN。sem_trywait()其实是sem_wait()的非阻塞版本。

sem_timedwait   还没弄清楚。

sem_destory   这个函数的作用是,用完信号量后对它进行清理,清理该信号量所拥有的资源。如果你试图清理的信号量正被一些线程等待,就会收到一个错误。与大多数Linux函数一样,这些函数在成功时都返回0。


通过互斥锁来同步线程,另一种用在多线程程序中同步访问的方法是使用互斥量。它允许程序员锁住某个对象,使得每次只能有一个线程访问它。为了控制对关键代码的访问,必须在进入这段代码之前锁住一个互斥量,然后在完成操作之后解锁它,简单程序如下:

#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <semaphore.h>#define SIZE 1024char buffer[SIZE];void *thread_function(void *arg);pthread_mutex_t mutex;int main(){    int res;    pthread_t a_thread;    void *thread_result;    res = pthread_mutex_init(&mutex, NULL);    if (res != 0)    {        perror("Mutex init failed!");        exit(EXIT_FAILURE);    }    res = pthread_create(&a_thread, NULL, thread_function, NULL);    if (res != 0)    {        perror("Thread create failed!");        exit(EXIT_FAILURE);    }    printf("Input some text. Enter 'end' to finish\n");    while (1)    {        pthread_mutex_lock(&mutex);        scanf("%s", buffer);        pthread_mutex_unlock(&mutex);        if (strncmp("end", buffer, 3) == 0)            break;        sleep(1);    }    res = pthread_join(a_thread, &thread_result);    if (res != 0)    {        perror("Thread join failed!");        exit(EXIT_FAILURE);    }    printf("Thread joined\n");    pthread_mutex_destroy(&mutex);    exit(EXIT_SUCCESS);}void *thread_function(void *arg){    sleep(1);    while (1)    {        pthread_mutex_lock(&mutex);        printf("You input %d characters\n", strlen(buffer));        pthread_mutex_unlock(&mutex);        if (strncmp("end", buffer, 3) == 0)            break;        sleep(1);    }}

与其他函数一样,成功时返回0,失败时将返回错误代码,但这些函数并不设置errno,所以必须对函数的返回代码进行检查。互斥量的属性设置这里不讨论,因此设置成NULL

0 0
原创粉丝点击