c - 运行 2 个线程与一个共享变量并行
c - running 2 threads in parallel with a shared variable
只是线程的初学者,我正在做一个涉及这两个线程的任务。
#include <stdio.h>
#include <pthread.h>
int count = 0;
void waitFor(unsigned int secs)
{
unsigned int retTime = time(0) + secs;
while(time(0) < retTime);
}
void func1(void * args)
{
printf("In func1 ...\n");
long i = 0;
while(1){
i++;
if(count == 1)
break;
}
printf("The total number counted is: %ld \n", i);
count = 0;
i = 0;
}
void func2(void * args)
{
printf("In func2 ...\n");
waitFor(3);
count = 1;
}
int main()
{
pthread_t th1, th2;
int j = 0;
while(j++ < 4){
printf("\nRound:\t%d\n", j);
pthread_create(&th1, NULL, (void*)func1,NULL);
pthread_create(&th2, NULL, (void*)func2, NULL);
pthread_join(th1,NULL);
pthread_join(th2,NULL);
waitFor(3);
}
return 0;
}
我已经阅读了各种参考资料,据我了解 pthread_join() 意味着如果有 2 个或更多线程,那么它们将等待一个线程完成其执行,然后下一个线程将开始执行等等。
但是当我运行这个程序的时候,pthread_join(th1)执行的那一刻,两个线程都被创建并执行了'concurrently'。这是怎么回事?
输出:
Round: 1
In func2 ...
In func1 ...
The total number counted is: 897651254
Round: 2
In func1 ...
In func2 ...
The total number counted is: 1051386065
........
我的目标是 运行 这 2 个线程并行。目前,join 似乎可以做到这一点;还是我哪里出错了?
而且我读到 C 中的线程不推荐使用 volatile。那么有什么方法可以使用计数作为从线程 2 到线程 1 的信号吗?
引用:
my understanding pthread_join() means that if there are 2 or more threads, then they will wait for one thread to finish its execution and then next one will start executing and so on
这是不正确的。加入只是意味着进程等待直到线程终止。
引用:
the moment pthread_join(th1) is executed, both threads are created and executed 'concurrently'.
这是不正确的。线程在调用 pthread_create
时创建并启动 注意:start 我的意思是它们已准备好执行。然而,正是 OS 决定了它们何时真正开始执行,因此它们可能需要一些时间才能执行。
要在两个线程之间共享 count
,您可以使用互斥锁。
int count = 0;
pthread_mutex_t lock;
访问count
时必须先锁定互斥量,read/write变量然后解锁互斥量。
示例:
pthread_mutex_lock(&lock);
count = 1;
pthread_mutex_unlock(&lock);
示例:
long i = 0;
while(1)
{
... code not using count ....
pthread_mutex_lock(&lock);
if(count == 1)
{
pthread_mutex_unlock(&lock);
break;
}
pthread_mutex_unlock(&lock);
... code not using count ....
}
并且在 main
中,您需要像这样初始化互斥量:
pthread_mutex_init(&lock,NULL);
只是线程的初学者,我正在做一个涉及这两个线程的任务。
#include <stdio.h>
#include <pthread.h>
int count = 0;
void waitFor(unsigned int secs)
{
unsigned int retTime = time(0) + secs;
while(time(0) < retTime);
}
void func1(void * args)
{
printf("In func1 ...\n");
long i = 0;
while(1){
i++;
if(count == 1)
break;
}
printf("The total number counted is: %ld \n", i);
count = 0;
i = 0;
}
void func2(void * args)
{
printf("In func2 ...\n");
waitFor(3);
count = 1;
}
int main()
{
pthread_t th1, th2;
int j = 0;
while(j++ < 4){
printf("\nRound:\t%d\n", j);
pthread_create(&th1, NULL, (void*)func1,NULL);
pthread_create(&th2, NULL, (void*)func2, NULL);
pthread_join(th1,NULL);
pthread_join(th2,NULL);
waitFor(3);
}
return 0;
}
我已经阅读了各种参考资料,据我了解 pthread_join() 意味着如果有 2 个或更多线程,那么它们将等待一个线程完成其执行,然后下一个线程将开始执行等等。
但是当我运行这个程序的时候,pthread_join(th1)执行的那一刻,两个线程都被创建并执行了'concurrently'。这是怎么回事? 输出:
Round: 1
In func2 ...
In func1 ...
The total number counted is: 897651254
Round: 2
In func1 ...
In func2 ...
The total number counted is: 1051386065
........
我的目标是 运行 这 2 个线程并行。目前,join 似乎可以做到这一点;还是我哪里出错了?
而且我读到 C 中的线程不推荐使用 volatile。那么有什么方法可以使用计数作为从线程 2 到线程 1 的信号吗?
引用:
my understanding pthread_join() means that if there are 2 or more threads, then they will wait for one thread to finish its execution and then next one will start executing and so on
这是不正确的。加入只是意味着进程等待直到线程终止。
引用:
the moment pthread_join(th1) is executed, both threads are created and executed 'concurrently'.
这是不正确的。线程在调用 pthread_create
时创建并启动 注意:start 我的意思是它们已准备好执行。然而,正是 OS 决定了它们何时真正开始执行,因此它们可能需要一些时间才能执行。
要在两个线程之间共享 count
,您可以使用互斥锁。
int count = 0;
pthread_mutex_t lock;
访问count
时必须先锁定互斥量,read/write变量然后解锁互斥量。
示例:
pthread_mutex_lock(&lock);
count = 1;
pthread_mutex_unlock(&lock);
示例:
long i = 0;
while(1)
{
... code not using count ....
pthread_mutex_lock(&lock);
if(count == 1)
{
pthread_mutex_unlock(&lock);
break;
}
pthread_mutex_unlock(&lock);
... code not using count ....
}
并且在 main
中,您需要像这样初始化互斥量:
pthread_mutex_init(&lock,NULL);