【发布时间】:2020-03-09 14:21:11
【问题描述】:
在以下代码中,我尝试使用多个线程处理各种链表操作。可以支持多个链表,并且所有函数都是通用的,除了我保留了一些 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;
}
输出为
Thread: 0
The position to be inserted is 3
After insertion
ll: 2 15 79 8
Thread: 1
The position to be deleted is 1
Before deletion
ll: 2 15 -2087655584 8
ll: 15 -2087655584 8
显然,insert_mid 函数将 79 插入到位置 2,但为什么它会变为 -2087655584?解决办法是什么?
如果需要任何信息,请告知。
【问题讨论】:
-
您没有将源发布到 create()。当你这样做的时候,为什么不添加一些代码来检查 pthread_rwlock_{rdlock,wrlock,unlock}() 的返回值。尝试想象如果这些线程返回错误而不是暂停执行,您的线程会做什么。此外,对同一个操作进行写锁和读锁是没有意义的;为修改而写,为查看而阅读。 (这就是为什么我怀疑你没有在 create 中执行 pthread_rwlock_init(),所以这些函数是无操作的)。
-
我不知道这是否与你的问题有关,但你不应该同时获得读锁和写锁。如果您只想读取数据,则获得读锁,如果您想写入,则获得写锁,然后解锁一次。检查返回值是否成功获取锁。如果你不持有锁,解锁的行为是不确定的。
-
请显示
new_node函数。很确定你保留了一个指向 v 的指针,它分配在你创建的第一个线程的堆栈上。 -
@Bodo 是的,你是对的。这里只需要 write_lock。我只是想调试,所以我想为什么不暂停列表上的所有操作,因为显然读取操作失败(读取错误值)
-
@mevets 我在编辑中包含了 create() 函数。我以前做过 pthread_rwlock_init() 。获取锁的返回值为0,表示一切正常。
标签: c multithreading linked-list critical-section readwritelock