Chinaunix首页 | 论坛 | 博客
  • 博客访问: 24380
  • 博文数量: 5
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 14
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-16 20:39
文章分类
文章存档

2019年(1)

2017年(1)

2014年(1)

2013年(2)

我的朋友
最近访客

分类: LINUX

2019-03-20 10:13:43

此篇文章讲述如何在PC机在eclipse环境下使用armeb-adv-linux-gnu-gdb交叉调试arm上的多线程程序。
代码如下:

#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10

pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;

void *thread1()
{
        printf ("thread1 : I'm thread 1\n");

        for (i = 0; i < MAX; i++)
        {
                printf("thread1 : number = %d\n",number);
                pthread_mutex_lock(&mut);
                        number++;
                pthread_mutex_unlock(&mut);
                sleep(2);
        }
        pthread_exit(NULL);
}

void *thread2()
{
        printf("thread2 : I'm thread 2\n");

        for (i = 0; i < MAX; i++)
        {
                printf("thread2 : number = %d\n",number);
                pthread_mutex_lock(&mut);
                        number++;
                pthread_mutex_unlock(&mut);
                sleep(3);
        }
        pthread_exit(NULL);
}

void thread_create(void)
{
        int temp;
        memset(&thread, 0, sizeof(thread)); //comment1

        /*创建线程*/
        if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2

                printf("create thread 1 failed\n");
        else
                printf("thread 1 created successfully\n");

        if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3

            printf("create thread 2 failed\n");
        else
            printf("thread 2 created successfully\n");
}

void thread_wait(void)
{
        /*等待线程结束*/
        if(thread[0] !=0) { //comment4

                pthread_join(thread[0],NULL);
                printf("thread 1 ends\n");
        }
        if(thread[1] !=0) { //comment5

                pthread_join(thread[1],NULL);
                printf("thread 2 ends\n");
        }
}

int main()
{
        /*用默认属性初始化互斥锁*/
        pthread_mutex_init(&mut,NULL);

        printf("I am the main thread, I am creating threads\n");
        thread_create();
        printf("I am the main thread, I am waiting threads\n");
        thread_wait();

        return 0;
}

注:以上代码摘自网络

makefile内容如下:
armeb-adv-linux-gnu-gcc.exe -g -o st main.c -lpthread --static
使用--static选项是为了防止调试时找不到库

调试过程如下:

1.在main处设置断点并执行 continue

2.执行 set scheduler-locking on
含义:执行step或continue命令时,只有当前线程会执行

3.到thread_create执行完时,共创建了三个线程:

4.执行 "break main.c:13 thread 2",然后执行 "thread 2"可以切换到第2个线程执行(按thread之前的标号记)

5.执行 "next"命令单步调试第2个线程:

6.执行 "break main.c:28 thread 3",然后执行 "thread 3",切换到第3个线程执行:



阅读(1326) | 评论(0) | 转发(0) |
0

上一篇:APM源码学习笔记-更新1

下一篇:没有了

给主人留下些什么吧!~~