C 为链表上的操作实现多线程时出现问题
在下面的代码中,我尝试使用多个线程处理各种链表操作。可以支持多个链表,所有函数都是通用的,除了我保留了一些C 为链表上的操作实现多线程时出现问题,c,multithreading,linked-list,critical-section,readwritelock,C,Multithreading,Linked List,Critical Section,Readwritelock,在下面的代码中,我尝试使用多个线程处理各种链表操作。可以支持多个链表,所有函数都是通用的,除了我保留了一些printf语句来调试代码 typedef void (*gen_fun_t)(void *); ll_t thread[2]; int ret; #define RWLOCK(lt, lk) (ret=(lt) == l_read)? pthread_rwlock_rdlock(&(lk)): pthread_rwlock_wrlock(&(lk)) #define R
printf
语句来调试代码
typedef void (*gen_fun_t)(void *);
ll_t thread[2];
int ret;
#define RWLOCK(lt, lk) (ret=(lt) == l_read)? pthread_rwlock_rdlock(&(lk)): pthread_rwlock_wrlock(&(lk))
#define RWUNLOCK(lk) pthread_rwlock_unlock(&(lk));
typedef enum locktype locktype_t;
enum locktype
{
l_read,
l_write
};
struct node
{
void *val;
struct node *next;
pthread_rwlock_t m;
};
struct ll
{
int len;
struct node *head;
pthread_rwlock_t m;
gen_fun_t val_teardown;
gen_fun_t val_printer;
};
typedef struct ll* ll_t;
typedef struct node * ll_node;
ll_t create( gen_fun_t val_teardown)
{
ll_t list=(ll_t)malloc(sizeof(struct ll));
list->head=NULL;
list->len=0;
list->val_teardown = val_teardown;
pthread_rwlock_init(&list->m, NULL);
return list;
}
ll_node new_node(void *val)
{
ll_node node= (ll_node)malloc(sizeof(struct node));
node->val=val;
node->next=NULL;
pthread_rwlock_init(&node->m, NULL);
return node;
}
int remove_mid(ll_t list, int n)
{
printf("Before deletion \n");
print(list);
ll_node temp;
if(n==0)
{
temp=list->head;
list->head=temp->next;
printf("%d \n", *(int *)(list->head)->val);
}
else
{
ll_node it;
it=list->head;
for(;n>1;n--)
it=it->next;
printf("%d \n", *(int *)(it->next)->val);
temp=it->next;
it->next=it->next==NULL? NULL: temp->next;
printf("%d \n", *(int *)(it->next)->val);
}
(list->len)--;
list->val_teardown(temp->val);
free(temp);
return list->len;
}
int insert_mid(ll_t list, void *val, int n)
{
ll_node node= new_node(val);
if(n==0)
{
node->next=list->head;
list->head=node;
}
else
{
ll_node it;
it=list->head;
for(;n>1;n--)
it=it->next;
node->next=it->next;
it->next=node;
printf("After insertion \n");
print(list);
printf("\n");
}
(list->len)++;
return list->len;
}
void *thread_operation(void * arg)
{
long id= (long) arg;
if(id==0)
{
RWLOCK(l_write, list[0]->m);
//RWLOCK(l_read, list[0]->m);
printf("The status of lock operation is %d \n", ret);
printf("\nThread: %ld \n", id);
int v=rand()%100;
int pos=rand()%list[0]->len;
printf("The position inserted is %d \n",pos+1);
pos=insert_mid(list[0], &v, pos);
//RWUNLOCK(list[0]->m);
RWUNLOCK(list[0]->m);
}
else
{
RWLOCK(l_write, list[0]->m);
//RWLOCK(l_read, list[0]->m);
printf("The status of lock operation is %d \n", ret);
printf("\nThread: %ld \n", id);
int pos=rand()%list[0]->len;
printf("The position to be deleted is %d \n", pos+1);
pos=remove_mid(list[0], pos);
print(list[0]);
//RWUNLOCK(list[0]->m);
RWUNLOCK(list[0]->m);
}
}
int main()
{
int thread_count=2;
long thread;
srand(time(0));
list[0]= create(int_tear);
list[0]->val_printer = int_printer;
list[1]=create(float_tear);
list[1]->val_printer= float_printer;
pthread_t *thread_handles;
int l, a=2, b=8, c=15;
l=insert_first(list[0], &a);
l=insert_end(list[0], &b);
l=insert_mid(list[0], &c, rand()%l);
double start, finish, elapsed;
start=clock();
thread_handles= (pthread_t *) malloc(thread_count*sizeof(pthread_t));
for(thread=0;thread<thread_count;thread++)
pthread_create(&thread_handles[thread], NULL, thread_operation, (void *)thread);
for(thread=0;thread<thread_count;thread++)
pthread_join(thread_handles[thread], NULL);
finish=clock();
elapsed =(finish-start)/CLOCKS_PER_SEC;
return 0;
}
显然,79是通过insert_mid
函数插入到位置2的,但为什么它会变为-2087655584?解决办法是什么
如果需要任何信息,请通知。简短摘要:您将局部变量的地址存储在列表节点中,而此变量超出范围,因此地址无效
函数
new_节点(void*val)
获取一个地址作为参数,并将该地址作为node->val
存储在节点结构中
在main
函数中,首先使用局部变量的地址创建3个节点
int l, a=2, b=8, c=15;
l=insert_first(list[0], &a);
l=insert_end(list[0], &b);
l=insert_mid(list[0], &c, rand()%l);
这些变量在main
结束之前有效,因此这里没有问题
在这个循环中
for(thread=0;thread<thread_count;thread++)
pthread_create(&thread_handles[thread], NULL, thread_operation, (void *)thread);
当线程\u操作
离开
if(id==0)
{
/* ... */
}
变量超出范围并变为无效。(如果编译程序时未进行优化,则在函数返回之前,程序可能会保持其值,但这并不能保证。)
当thread\u operation
返回时,该变量所在的堆栈区域将被释放,并将用于以后的其他函数调用
打印列表内容时,ID为0的线程插入的节点指向堆栈上的某个地址,该地址曾经包含变量v
,可能正在使用中,也可能在之后用于其他用途。这就是该值被更改的原因。这是未定义的行为
要解决此问题,您可以将节点结构更改为将变量的值存储为int
,而不是将变量的地址存储为void*
,或者您必须分配内存并创建数据的副本,如下所示
ll_node new_node(void *val, size_t size)
{
ll_node node= (ll_node)malloc(sizeof(struct node));
/* TODO check that malloc did not return NULL */
node->val = malloc(size);
/* TODO check that malloc did not return NULL */
memcpy(node->val, val, size);
node->next=NULL;
pthread_rwlock_init(&node->m, NULL);
return node;
}
其他可能的问题:
在每个列表节点中创建一个锁对象。如果要使用它来保护对节点中数据的访问,则相应线程必须(至少)获取列表的读锁,以防止其他线程删除节点。您没有将源发布到create()。在执行此操作时,为什么不添加一些代码来检查pthread_rwlock_{rdlock,wrlock,unlock}()的返回值呢。试着想象一下,如果这些线程返回错误,而不是暂停执行,您的线程会做什么。而且,在同一操作上执行写锁和读锁也没有意义;写是为了修改,读是为了看。(这就是为什么我怀疑您没有在create中执行pthread_rwlock_init(),因此这些函数不是ops)。我不知道这是否与您的问题有关,但您不应该同时获得读锁和写锁。如果您只想读取数据,请获取读取锁定;如果您想写入,请获取写入锁定,然后解锁一次。检查返回值以查看是否成功获取了锁。如果未持有锁,则解锁行为未定义。请显示
new_节点
功能。非常确定您保留了一个指向v的指针,它是在您创建的第一个线程的堆栈上分配的。@Bodo是的,您是对的。这里只需要写锁。我只是想调试,所以我想为什么不暂停列表上的所有操作,因为读取操作显然失败了(读取错误的值)@mevets我在编辑中包含了create()函数。我以前做过pthread\u rwlock\u init()。获取锁的返回值为0,表示一切正常,谢谢您的解释。
if(id==0)
{
/* ... */
}
ll_node new_node(void *val, size_t size)
{
ll_node node= (ll_node)malloc(sizeof(struct node));
/* TODO check that malloc did not return NULL */
node->val = malloc(size);
/* TODO check that malloc did not return NULL */
memcpy(node->val, val, size);
node->next=NULL;
pthread_rwlock_init(&node->m, NULL);
return node;
}