아래의 내 의사 코드를 참조하십시오. 코드 주석이 내 문제를 설명해야합니다. 나는 pthread와 C의 링크드리스트에 익숙하지 않아서 깊은 곳으로 뛰어 들었습니다. 그냥 thread_work
함수에서 str
값을 출력해야합니다. 순차 코드 비트는 괜찮지 만 각 스레드가 작업 할 때 str
값을 인쇄 할 수 없습니다. 내 실제 코드에서연결된 목록 노드를 pthread 함수에 올바르게 전달
// linked list definition
struct linked_list {
char *str;
struct linked_list *next;
};
// linked list initiation
struct linked_list *root;
struct linked_list *next_info;
root = malloc(sizeof(struct linked_list));
// main code
some loop {
next_node->str = str;
printf("%s\n", next_node); // PRINTS FINE
pthread_t thread;
rc = pthread_create(&thread, NULL, thread_work, (void *) &next_node);
next_node->next = malloc(sizeof(struct linked_list));
next_node = next_node->next;
}
// code executed by each thread
void *thread_work(void *thread_arg) {
struct linked_list *ll;
ll = (struct linked_list *)thread_arg;
printf("%s\n", ll->str); // PRINTS SOME MESS (��E#)
}
는 linked_list
struct
의 몇 가지 더 많은 회원이 있습니다.
감사합니다.
그냥 참고 : 당신은하지의 주소,
next_node
의 값을 전달해야 thread_arg'은''무효 * 인'때문에, 다른 데이터 포인터 형식으로 변환 할 때 캐스팅 절대적으로 필요가있다. 'struct linked_list * ll = thread_arg; "'. – unwind