小编wsn*_*ski的帖子

在20行程序中创建C线程.为什么不起作用?

我正在尝试运行一个简短的程序,在for循环中创建三个线程,每个线程都在屏幕内"写入".Cygwin在不同的机器上运行XP和Vista都会发生这种情况.这是当前的代码.

#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
using namespace std;
void* printInside(void* arg);

int main()
{
    pthread_t threads[3];
    for(int i = 0; i < 3; i++)
    {
        pthread_create(&threads[i], 0, printInside, 0);
    }
    return 0;
}
void* printInside(void* arg)
{
    cout << "inside";
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

它不起作用.如果我在for循环的内部添加一个cout,它似乎会减慢它的工作速度.

for(int i = 0; i < 3; i++)
{
    cout << "";
    pthread_create(&threads[i], 0, printInside, 0);
}
Run Code Online (Sandbox Code Playgroud)

有关为什么会出现这种情况的任何建议?

编辑:

我已经得到了在循环后添加连接的响应

int main()
 {
     pthread_t threads[3];
     for(int i = 0; i < 3; …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading

9
推荐指数
1
解决办法
225
查看次数

标签 统计

c++ ×1

multithreading ×1