请在下面查看我的伪代码。代码注释应该解释我的问题。我对 C 中的 pthreads 和链表都是新手,所以我已经跳入了深层次。我只需要str
在函数中打印 out 的值thread_work
。代码的顺序位很好,但是当每个线程执行其工作时,它无法打印出str
.
// 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
.
非常感谢。