在 C 中使用线程。得到意想不到的结果
Using threads in C. Getting unexpected results
我刚接触 C 中的线程。
所以从最基础的开始。
我只是想创建线程并对它们使用互斥。
我已经声明了三个函数并为它们创建了 3 个线程,但是每次我执行我的程序时,并不是所有三个进程都一直执行
请给出解决方案。
您必须在 pthread_join()
中传递线程对象,请参阅更新的第 35、38、41 行。
函数的定义是int pthread_join(pthread_t thread, void **retval);
这会有所帮助。
34 thread1=pthread_create(&trd1,NULL,process1,(void *)nargs1);
35 pthread_join(trd1, NULL);
36
37 thread2=pthread_create(&trd2,NULL,process2,(void *)nargs2);
38 pthread_join(trd2, NULL);
39
40 thread3=pthread_create(&trd3,NULL,process3,(void *)nargs3);
41 pthread_join(trd3, NULL);
我刚接触 C 中的线程。 所以从最基础的开始。 我只是想创建线程并对它们使用互斥。 我已经声明了三个函数并为它们创建了 3 个线程,但是每次我执行我的程序时,并不是所有三个进程都一直执行
请给出解决方案。
您必须在 pthread_join()
中传递线程对象,请参阅更新的第 35、38、41 行。
函数的定义是int pthread_join(pthread_t thread, void **retval);
这会有所帮助。
34 thread1=pthread_create(&trd1,NULL,process1,(void *)nargs1);
35 pthread_join(trd1, NULL);
36
37 thread2=pthread_create(&trd2,NULL,process2,(void *)nargs2);
38 pthread_join(trd2, NULL);
39
40 thread3=pthread_create(&trd3,NULL,process3,(void *)nargs3);
41 pthread_join(trd3, NULL);