Chinaunix首页 | 论坛 | 博客
  • 博客访问: 484391
  • 博文数量: 285
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 629
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-14 17:53
个人简介

相信自己,快乐每一天

文章分类

全部博文(285)

分类: LINUX

2013-12-19 00:24:56

/* thread_example.c
*/
#include
#include
#include
#include

#define MAX 10

pthread_t thread[2];
pthread_mutex_t mut;
int num = 0;
int i = -1;

void *thread1()
{
    printf("thread1: l am thread 1\n");
    for(i = 0; i < MAX; i++)
    {
        printf("thread1:num = %d\n",num);
        pthread_mutex_lock(&mut);
        num++;
        pthread_mutex_unlock(&mut);
        sleep(2);
    }
    printf("thread1: master function waiting for me to done?\n");
    pthread_exit(NULL);
}
void *thread2()
{
    printf("thread2: l am thread2\n");
    for(i = 0; i < MAX; i++)
    {
        printf("thread2:num = %d\n",num);
        pthread_mutex_lock(&mut);
        num++;
        pthread_mutex_unlock(&mut);
        sleep(3);
    }
    printf("thread2: master function is waiting for me? \n");
    pthread_exit(NULL);
}

void thread_create(void)
{
    int temp = -1;
    memset(&thread, 0, sizeof(thread));
    if( 0 != (temp = pthread_create(&thread[0], NULL, thread1, NULL)) )
    {
        printf("create thread1 failed\n");
    }
    else
    {
        printf("create thread1 success\n");
    }

    if( 0 != (temp = pthread_create(&thread[1], NULL, thread2, NULL)) )
    {
        printf("create thread2 failed\n");
    }
    else
    {
        printf("create thread2 success\n");
    }
}

void thread_wait(void)
{
    if(0 != thread[0])
    {
        pthread_join(thread[0], NULL);
        printf("now thread1 is over\n");
    }
    if(0 != thread[1])
    {
        pthread_join(thread[1], NULL);
        printf("now thread2 is over\n");
    }
}

int main()
{
    pthread_mutex_init(&mut, NULL);
    printf("l am master function \n");
    thread_create();
    printf("l am master function,l am waiting for threads to over\n");
    thread_wait();
    return 0;
}

gcc -o thread_example -c thread_example.c -pthread
./thread_example 执行程序结果如下:
l am master function
create thread1 success
thread1: l am thread 1
create thread2 success
l am master function,l am waiting for threads to over
thread2: l am thread2
thread1:num = 0
thread2:num = 0
thread1:num = 2
thread2:num = 3
thread1:num = 4
thread2:num = 5
thread1:num = 6
thread1:num = 7
thread2:num = 8
thread1:num = 9
thread2:num = 10
thread1: master function waiting for me to done?
now thread1 is over
thread2: master function is waiting for me?
now thread2 is over

再次执行程序结果如下:
l am master function
create thread1 success
thread1: l am thread 1
create thread2 success
l am master function,l am waiting for threads to over
thread1:num = 0
thread2: l am thread2
thread2:num = 1
thread1:num = 2
thread2:num = 3
thread1:num = 4
thread2:num = 5
thread1:num = 6
thread1:num = 7
thread2:num = 8
thread1:num = 9
thread2:num = 10
thread1: master function waiting for me to done?
now thread1 is over
thread2: master function is waiting for me?
now thread2 is over
再次结果是不一样的:对于num的第一次的值,当thread1中num =0时,thread2中的num 就不应该再出现num = 0的可能了,但却出现了,反复执行程序,结果如上所示。
不知道这是为什么,请高手指点,谢谢。
线程相关的一些操作:
pthread_t-----> typedef unsigned long int pthread_t
pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)

*thread:    指向线程start_rountine的ID

*attr:    线程start_rountine的属性值,

start_rountine:    待创建的新的线程

*arg:    传递给start_rountine的参数

线程的两种结束方式:一种是线程自己运行完了之后的结束,另外一种是采用pthread_exit结束

当线程还没有执行完的时候,有时候需要等待线程的结束,采用pthread_join()来等待线程的结束
int (pthread_t thread, void **retval);
pthread_t thread:指向线程的ID号
void **retval:用来保存线程执行后返回的值
当pthread_join()成功时返回0,否则,返回错误代码
把程序修改一下,也就是让线程结束pthread_exit(“XXXXXXX”)传递一些东西出去,如:
/* thread_example.c
*/
#include
#include
#include
#include

#define MAX 10

pthread_t thread[2];
pthread_mutex_t mut;
int num = 0;
int i = -1;

void *thread1()
{
    printf("thread1: l am thread 1\n");
    for(i = 0; i < MAX; i++)
    {
        printf("thread1:num = %d\n",num);
        pthread_mutex_lock(&mut);
        num++;
        pthread_mutex_unlock(&mut);
        sleep(2);
    }
    printf("thread1: master function waiting for me to done?\n");
    pthread_exit("------------------thread1\n");
}
void *thread2()
{
    printf("thread2: l am thread2\n");
    for(i = 0; i < MAX; i++)
    {
        printf("thread2:num = %d\n",num);
        pthread_mutex_lock(&mut);
        num++;
        pthread_mutex_unlock(&mut);
        sleep(3);
    }
    printf("thread2: master function is waiting for me? \n");
    pthread_exit("this is thread2----------->\n");
}

void thread_create(void)
{
    int temp = -1;
    memset(&thread, 0, sizeof(thread));
    if( 0 != (temp = pthread_create(&thread[0], NULL, thread1, NULL)) )
    {
        printf("create thread1 failed\n");
    }
    else
    {
        printf("create thread1 success\n");
    }

    if( 0 != (temp = pthread_create(&thread[1], NULL, thread2, NULL)) )
    {
        printf("create thread2 failed\n");
    }
    else
    {
        printf("create thread2 success\n");
    }
}

void *join1 = NULL;
void *join2 = NULL;
void thread_wait(void)
{
    if(0 != thread[0])
    {
        pthread_join(thread[0], &join1);
        printf("thread1 return value is:%s\n",join1);
        printf("now thread1 is over\n");
    }
    if(0 != thread[1])
    {
        pthread_join(thread[1], &join2);
        printf("thread2 return value is:%s\n",join2);
        printf("now thread2 is over\n");
    }
}

int main()
{
    pthread_mutex_init(&mut, NULL);
    printf("l am master function \n");
    thread_create();
    printf("l am master function,l am waiting for threads to over\n");
    thread_wait();
    return 0;
}

这个时候返回值就有结果了,运行一下可以看到返回值的结果。

阅读(535) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~