通过两个线程实现数组倒置,线程一用于循环倒置,线程二用于循环打印。用互斥锁实现同步, int a[10] = {0,1,2,3,4,5,6,7,8,9};
时间: 2023-08-19 21:28:47 浏览: 59
好的,下面是代码实现:
```c++
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int a[10] = {0,1,2,3,4,5,6,7,8,9};
mutex m;
void reverse() {
m.lock(); // 加锁
int temp;
for (int i = 0; i < 5; i++) {
temp = a[i];
a[i] = a[9-i];
a[9-i] = temp;
}
m.unlock(); // 解锁
}
void print() {
m.lock(); // 加锁
for (int i = 0; i < 10; i++) {
cout << a[i] << " ";
}
cout << endl;
m.unlock(); // 解锁
}
int main() {
thread t1(reverse);
thread t2(print);
t1.join();
t2.join();
return 0;
}
```
以上代码通过两个线程实现了数组的倒置和打印,其中使用了互斥锁来保证线程同步。线程一负责倒置数组,线程二负责打印数组内容。在访问共享变量 a 时,使用了互斥锁来保证线程安全。
相关问题
.通过两个线程实现数组倒置,线程一用于循环倒置,线程二用于循环打印。用互斥锁实现同步,
下面是一个使用互斥锁实现两个线程实现数组倒置和打印的示例代码:
```c
#include <stdio.h>
#include <pthread.h>
#define N 10
int array[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
pthread_mutex_t lock;
void *reverse_array(void *arg) {
int i, tmp;
pthread_mutex_lock(&lock);
for (i = 0; i < N / 2; i++) {
tmp = array[i];
array[i] = array[N - i - 1];
array[N - i - 1] = tmp;
}
pthread_mutex_unlock(&lock);
pthread_exit(NULL);
}
void *print_array(void *arg) {
int i;
pthread_mutex_lock(&lock);
printf("Reverse array: ");
for (i = 0; i < N; i++) {
printf("%d ", array[i]);
}
printf("\n");
pthread_mutex_unlock(&lock);
pthread_exit(NULL);
}
int main() {
pthread_t t1, t2;
pthread_mutex_init(&lock, NULL);
pthread_create(&t1, NULL, reverse_array, NULL);
pthread_create(&t2, NULL, print_array, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&lock);
return 0;
}
```
在上面的代码中,线程一执行数组倒置操作,线程二执行数组打印操作。在两个线程中都使用了 pthread_mutex_lock() 和 pthread_mutex_unlock() 函数对互斥量进行加锁和解锁操作,以保证同一时间只有一个线程能够访问共享资源。注意,在加锁期间,如果另一个线程也想要访问该互斥量,就会被阻塞,直到加锁的线程解锁该互斥量。最终,通过调用 pthread_join() 函数,等待线程结束,释放线程资源,销毁互斥量资源。
在ubuntu当中使用互斥锁实现两个线程分别打印“hello” “world”
好的,这是一个技术问题,我可以为您提供帮助。在 Ubuntu 中,您可以使用 pthread 库的互斥锁来实现线程同步。以下是一个示例代码,可供参考:
```c
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex; //定义互斥锁
void* print_hello(void* arg) {
pthread_mutex_lock(&mutex); //加锁
printf("hello ");
fflush(stdout); //刷新缓冲区,确保输出
pthread_mutex_unlock(&mutex); //解锁
return NULL;
}
void* print_world(void* arg) {
pthread_mutex_lock(&mutex); //加锁
printf("world\n");
fflush(stdout); //刷新缓冲区,确保输出
pthread_mutex_unlock(&mutex); //解锁
return NULL;
}
int main(int argc, char* argv[]) {
pthread_t tid1, tid2;
pthread_mutex_init(&mutex, NULL); //初始化互斥锁
pthread_create(&tid1, NULL, print_hello, NULL); //创建线程
pthread_create(&tid2, NULL, print_world, NULL);
pthread_join(tid1, NULL); //等待线程结束
pthread_join(tid2, NULL);
pthread_mutex_destroy(&mutex); //销毁互斥锁
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)