【问题标题】:Thread synchronization to print sequential out put from multiple threads in c program线程同步以打印 c 程序中多个线程的顺序输出
【发布时间】:2017-10-12 03:13:11
【问题描述】:

编写一个c程序来实现两个线程,一个应该在循环中打印来自Arr1[]的字符

and other
 should print numbers from Arr2[] in loop?
Input :
 char Arr1[6] = {'a','b','c','d','e','f'};
 int Arr2[6] = {1,2,3,4,5,6}
Output :
 a1b2c3d4e5f6
 a1b2c3d4e5f6
 a1b2c3d4e5f6
 a1b2c3d4e5f6....

如何在上述程序中进行同步以打印所需的输出?

我尝试过使用线程同步技术,例如我使用过的 mutex,但它没有按照输出要求显示

请帮我找到解决办法

我在互联网上遇到了类似的问题,但这些问题是用 java 或 c++ 实现的,我希望它用 c 实现

【问题讨论】:

标签: c multithreading synchronization


【解决方案1】:

你可以使用 2 个互斥锁吗?所以,如果它们像 1 0 和其他 0 1 一样,首先可以写。每个都只是切换两个互斥锁 - 启用其他线程,并禁用它的线程。

【讨论】:

    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多