我正在尝试编写一个使用 3 个线程并打印 0、1、2、3、4、5、.... 等的程序。
线程 1 - 打印 0,3,6,9 等等
线程 2 - 打印 1,4,7,10 等等
线程 3 - 打印 2,5,8,11 等等
任何人都可以帮我编写代码吗?
我正在尝试编写一个使用 3 个线程并打印 0、1、2、3、4、5、.... 等的程序。
线程 1 - 打印 0,3,6,9 等等
线程 2 - 打印 1,4,7,10 等等
线程 3 - 打印 2,5,8,11 等等
任何人都可以帮我编写代码吗?
这里可能是更好的解决方案
#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
using namespace std;
#define MAX 10
condition_variable cv1, cv2, cv3;
mutex m1;
int i;
void function1()
{
while (i < MAX) {
unique_lock<mutex> lock(m1);
cv1.wait(lock);
cout << i << endl;
i++;
lock.unlock();
cv2.notify_one();
}
}
void function2()
{
while (i < MAX) {
unique_lock<mutex> lock(m1);
cv2.wait(lock);
cout << i << endl;
i++;
lock.unlock();
cv3.notify_one();
}
}
void function3()
{
while (i < MAX) {
unique_lock<mutex> lock(m1);
cv3.wait(lock);
cout << i << endl;
i++;
lock.unlock();
cv1.notify_one();
}
}
int main()
{
thread t3(function3);
thread t1(function1);
thread t2(function2);
cv1.notify_one();
t1.join();
t2.join();
t3.join();
getchar();
return 0;
}
一种可能的解决方案涉及消息队列。每个线程都有一个消息队列。
0
,然后发送0
到消息队列 2。线程 3 从消息队列 3 中读取值,将其递增,然后将新值发送到消息队列 1
从这里,您应该推断线程 1 下一步应该做什么。
以下程序使用条件变量进行同步。如果需要任何修改或发现任何错误,请帮助我。
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define THREAD_LIMIT 3
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int PRINT_MAX = 500;
pthread_cond_t cond[THREAD_LIMIT];// = PTHREAD_COND_INITIALIZER;
struct thread_arg
{
int index;
pthread_cond_t *waitCond;
pthread_cond_t *signalCond;
};
void* threadFun(void* pArg)
{
struct thread_arg *pThreadArg = (struct thread_arg *)pArg;
int i=pThreadArg->index, rValue;
for(;i< PRINT_MAX;i += THREAD_LIMIT)
{
rValue = pthread_mutex_lock(&mutex);
printf("thread index = %d value = %d \n",(pThreadArg->index+1), i);
rValue = pthread_cond_signal(pThreadArg->signalCond);
//wait for turn
while( pthread_cond_wait(pThreadArg->waitCond, &mutex) != 0 )
{}
rValue = pthread_mutex_unlock(&mutex);
}
rValue = pthread_cond_signal(pThreadArg->signalCond);
return NULL;
}
int main()
{
pthread_t ThreadId[THREAD_LIMIT];
struct thread_arg ThreadArg[THREAD_LIMIT];
for (int i =0; i< THREAD_LIMIT ; i++)
pthread_cond_init(&cond[i], NULL);
for (int i =0; i< THREAD_LIMIT ; i++)
{
ThreadArg[i].index = i;
ThreadArg[i].waitCond = &cond[i];
if (i == THREAD_LIMIT-1)
ThreadArg[i].signalCond = &cond[0];
else
ThreadArg[i].signalCond = &cond[i+1];
printf("starting Thread %d \n",i+1);
pthread_create(&ThreadId[i], NULL, &threadFun,(void*)&ThreadArg[i]);
usleep(500);
}
for (int i =0; i< THREAD_LIMIT ; i++)
pthread_join(ThreadId[i],NULL);
return 0;
}
具有 3 个线程的多线程程序,使用条件变量和互斥体打印数字序列。
什么是条件变量和互斥锁,何时使用以及如何使用? http://thispointer.com/c11-multithreading-part-7-condition-variables-explained/
#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
using namespace std;
//Creating 3 condition variable to perform synchronization in 3 different functions using wait and notify function on condition variable
condition_variable cv1, cv2, cv3;
//creating mutex variable to lock and unlock the resource
mutex m1;
void function1()
{
//creating unique_lock on the mutex so that we don't have to take care whether mutex is unlocked or not, as mutex gets unlocked as soon as unique_lock is out of scope (using concept of RAII)
unique_lock<mutex> lock(m1);
for(int i=0; i<10; i+=3)
{
cout<<i<<endl;
//We notify function2 using notify_one function, once we are done printing values from function one and makes function1 in wait state by calling wait function
cv2.notify_one();
cv1.wait(lock);
}
}
void function2()
{
unique_lock<mutex> lock(m1);
for(int i=1; i<10; i+=3)
{
cout<<i<<endl;
//We notify function3 using notify_one function, once we are done printing values from function2 and makes function2 in wait state by calling wait function
cv3.notify_one();
cv2.wait(lock);
}
}
void function3()
{
unique_lock<mutex> lock(m1);
for(int i=2; i<10; i+=3)
{
cout<<i<<endl;
//We notify function1 using notify_one function, once we are done printing values from function3 and makes function3 in wait state by calling wait function
cv1.notify_one();
cv3.wait(lock);
}
}
int main()
{
//creating 3 threads (t1, t2 & t3) and associating functions with each thread
thread t1(function1);
thread t2(function2);
thread t3(function3);
t1.join();
t2.join();
t3.join();
return 0;
}
输出:
0
1
2
3
4
5
6
7
8
9