c - 与共享变量并行运行2个线程

时间:2022-06-19 20:58:47

Just a beginner to threads, I'm just doing a task which involves these 2 threads.

作为线程的初学者,我只是在做一个涉及这两个线程的任务。

#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;
}

I've read various references and to 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.

我已经阅读了各种引用,并且根据我的理解,pthread_join()意味着如果有2个或更多线程,那么它们将等待一个线程完成其执行,然后下一个线程将开始执行,依此类推。

But when i run this program, the moment pthread_join(th1) is executed, both threads are created and executed 'concurrently'. How is this happening? Output:

但是当我运行这个程序时,执行pthread_join(th1)的那一刻,两个线程都被创建并“并发”执行。这是怎么回事?输出:

Round:  1
In func2 ...
In func1 ...
The total number counted is: 897651254 

Round:  2
In func1 ...
In func2 ...
The total number counted is: 1051386065

........

My goal is to run these 2 threads in parallel. For now, join seems to do this; or am I going wrong somewhere?

我的目标是并行运行这两个线程。现在,加入似乎是这样做的;或者我在某个地方出错?

And I've read that using volatile is not preferred for threads in C. So is there any way I could use count as a signal from thread 2 to 1?

而且我已经读过使用volatile不适合C中的线程。那么有什么方法可以将count用作线程2到1的信号?

1 个解决方案

#1


2  

Quote:

引用:

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

我的理解pthread_join()意味着如果有2个或更多线程,那么它们将等待一个线程完成其执行,然后下一个线程将开始执行,依此类推

That is incorrect. Join simply means that the process waits until the thread has terminated.

那是不对的。加入只是意味着进程等待直到线程终止。

Quote:

引用:

the moment pthread_join(th1) is executed, both threads are created and executed 'concurrently'.

在执行pthread_join(th1)的那一刻,两个线程都被“并发”地创建和执行。

That is incorrect. The threads are created and start when calling pthread_create Note: By start I mean that they are ready to execute. However, it is the OS that decides when they actually get to execute so it may take some time before they execute.

那是不对的。创建线程并在调用pthread_create时启动注意:开始时我的意思是它们已准备好执行。但是,操作系统决定何时实际执行,因此执行前可能需要一些时间。

To share count between two threads you can use a mutex.

要在两个线程之间共享计数,您可以使用互斥锁。

int count = 0;
pthread_mutex_t lock;

When accessing count you must first lock the mutex, read/write the variable and unlock the mutex.

访问计数时,必须首先锁定互斥锁,读取/写入变量并解锁互斥锁。

Example:

例:

pthread_mutex_lock(&lock);
count = 1;
pthread_mutex_unlock(&lock);

Example:

例:

pthread_mutex_lock(&lock);
if(count == 1) 
{
    pthread_mutex_unlock(&lock);
    break;
}
pthread_mutex_unlock(&lock);

And in main you'll need to initialize the mutex like:

在main中你需要初始化互斥量,如:

pthread_mutex_init(&lock,NULL);

#1


2  

Quote:

引用:

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

我的理解pthread_join()意味着如果有2个或更多线程,那么它们将等待一个线程完成其执行,然后下一个线程将开始执行,依此类推

That is incorrect. Join simply means that the process waits until the thread has terminated.

那是不对的。加入只是意味着进程等待直到线程终止。

Quote:

引用:

the moment pthread_join(th1) is executed, both threads are created and executed 'concurrently'.

在执行pthread_join(th1)的那一刻,两个线程都被“并发”地创建和执行。

That is incorrect. The threads are created and start when calling pthread_create Note: By start I mean that they are ready to execute. However, it is the OS that decides when they actually get to execute so it may take some time before they execute.

那是不对的。创建线程并在调用pthread_create时启动注意:开始时我的意思是它们已准备好执行。但是,操作系统决定何时实际执行,因此执行前可能需要一些时间。

To share count between two threads you can use a mutex.

要在两个线程之间共享计数,您可以使用互斥锁。

int count = 0;
pthread_mutex_t lock;

When accessing count you must first lock the mutex, read/write the variable and unlock the mutex.

访问计数时,必须首先锁定互斥锁,读取/写入变量并解锁互斥锁。

Example:

例:

pthread_mutex_lock(&lock);
count = 1;
pthread_mutex_unlock(&lock);

Example:

例:

pthread_mutex_lock(&lock);
if(count == 1) 
{
    pthread_mutex_unlock(&lock);
    break;
}
pthread_mutex_unlock(&lock);

And in main you'll need to initialize the mutex like:

在main中你需要初始化互斥量,如:

pthread_mutex_init(&lock,NULL);