多线程的调试比较麻烦,在linux下我们可以使用gdb来对多线程进行调试。gdb基本的使用不做介绍了。这里说一下使用gdb调试多线程的几种常用的辅助命令:
1. info threads: 显示当前可调试的线程,gdb会给每一个线程分配一个ID,ID前面带“*”的表示是当前被调试的线程
2. thread ID:调试目标ID指定的线程
3. set scheduler-locking [off | on | step]: 调试多线程时,默认除了被调试线程在执行外,其他线程也在执行,如果希望其他线程不执行的话,可以使用on这个选项。step的意思是在单步执行的时候,只有当前线程会执行。
下面使用一个代码实例,来说明如何调试多线程。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <assert.h>
typedef struct worker
{
void *(*process)(void *arg);
void *arg;
struct worker *next;
}CThread_worker;
typedef struct
{
pthread_mutex_t queue_lock;
pthread_cond_t queue_ready;
CThread_worker *queue_head;
int shutdown;
pthread_t *threadid;
int max_thread_num;
int cur_queue_size;
}CThread_pool;
static CThread_pool *pool = NULL;
int pool_add_worker(void*(*process)(void *arg), void *arg)
{
CThread_worker *newworker = (CThread_worker *) malloc(sizeof(CThread_worker));
newworker->process = process;
newworker->arg = arg;
newworker->next = NULL;
pthread_mutex_lock(&(pool->queue_lock));
CThread_worker *member = pool->queue_head;
if (member != NULL)
{
while (member->next != NULL)
member = member->next;
member->next = newworker;
}
else
{
pool->queue_head = newworker;
}
assert (pool->queue_head != NULL);
pool->cur_queue_size++;
pthread_mutex_unlock (&(pool->queue_lock));
pthread_cond_signal (&(pool->queue_ready));
return 0;
}
void *thread_routine(void *arg);
void pool_init(int max_thread_num)
{
pool = (CThread_pool *) malloc(sizeof(CThread_pool));
pthread_mutex_init(&(pool->queue_lock), NULL);
pthread_cond_init(&(pool->queue_ready), NULL);
pool->queue_head = NULL;
pool->max_thread_num = max_thread_num;
pool->cur_queue_size = 0;
pool->shutdown = 0;
pool->threadid = (pthread_t *)malloc(max_thread_num * sizeof(pthread_t));
for(int i = 0; i < max_thread_num; i++)
{
pthread_create(&(pool->threadid[i]), NULL, thread_routine, NULL);
}
}
void *thread_routine(void *arg)
{
printf("starting thread %ld \n", pthread_self());
while(1)
{
pthread_mutex_lock (&(pool->queue_lock));
while(pool->cur_queue_size == 0 && !pool->shutdown)
{
pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock));
}
/*等待队列长度减去1,并取出链表中的头元素*/
if(pool->cur_queue_size != 0 && pool->queue_head != NULL)
{
pool->cur_queue_size--;
CThread_worker *worker = pool->queue_head;
pool->queue_head = worker->next;
pthread_mutex_unlock (&(pool->queue_lock));
/*调用回调函数,执行任务*/
(*(worker->process)) (worker->arg);
free (worker);
worker = NULL;
}
}
}
void *
myprocess (void *arg)
{
printf ("threadid is %ld, working on task %d\n", pthread_self (),*(int *) arg);
sleep (1);/*休息一秒,延长任务的执行时间*/
return NULL;
}
int main()
{
pool_init(3);
/*连续向池中投入10个任务*/
int *workingnum = (int *) malloc (sizeof (int) * 10);
int i;
for (i = 0; i < 10; i++)
{
workingnum[i] = i;
pool_add_worker (myprocess, &workingnum[i]);
}
/*等待所有任务完成*/
sleep (10);
/*销毁线程池*/
//pool_destroy ();
free (workingnum);
return 0;
}
这里的代码参考的网上对于线程池的实现,主线程往队列中添加任务,线程池中的线程在条件变量下等待通知,如果有任务,被唤醒去执行任务。
这里注意生成可执行程序test时要加-g选项
启动gdb:gdb ./test

设置断点:在主线程中设置133行断点,然后在线程池中在108行设置断点
运行程序:首先在主函数的133行停下来

显示线程信息
1号线程是主线程,其余的是线程池中的线程。
现在单步往任务队列中添加任务。注意,这个时候其余的线程还是在运行。
这里注意,线程2已经被唤醒了,我们这时候想只看线程2的执行情况。需要使用命令:
set scheduler-locking on
然后使用命令:thread 2 进入2号线程池中的线程,可以看到它已经停在了断点108行处。
单步运行线程2:可以看到线程2的线程函数调用成功,成功返回。