这两者有什么区别?
它们是不是一样的,因为它们都在等待一个线程在执行另一个线程之前完成?
我正在尝试理解以下代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;
main()
{
int rc1, rc2;
pthread_t thread1, thread2;
/*Create independent threads each of which will execute functionC */
if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )
{
printf("Thread creation failed: %d\n", rc1);
}
if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) )
{
printf("Thread creation failed: %d\n", rc2);
}
/* Wait till threads are complete before main continues. Unless we */
/* wait we …Run Code Online (Sandbox Code Playgroud) 我正在用C编写一个程序来计算某些网站的访问时间.网站名称存储在urls数组的每个元素中.如果我取出for(y = 0; y <iterations; y ++)循环,那么一切运行正常.但是,如果我保留它.urls [0],第一个网站,在第二个for循环完全完成并增加y之后搞砸了
是什么导致了这个?
char *urls[50]; char str1[20];
void *wget(void *argument)
{
int threadid;
threadid = *((int *)argument);
strcpy(str1, "wget -q --spider ");
strcat(str1, urls[threadid]);
system(str1);
}
for (y = 0; y < iterations; y++)
{
for (j = 0; j < numthreads; j++)
{
thread_args[j] = j;
clock_gettime(CLOCK_REALTIME, &bgn);
rc = pthread_create(&threads[j], NULL, wget, (void *) &thread_args[j]);
rc = pthread_join(threads[j], NULL);
clock_gettime(CLOCK_REALTIME, &nd);
times[j] = timediff(bgn,nd);
}
}
Run Code Online (Sandbox Code Playgroud)