我以相当基本的方式实现了 PThreads:
#include<iostream>
#include<pthread.h>
#include<stdio.h>
using namespace std;
class ThreadParameter
{
public:
char symbol_char;
int count;
};
void* print_char (void* param)
{
ThreadParameter* p = (ThreadParameter*)param;
for (int i=0; i< p->count; i++)
{
cout<< p->symbol_char <<endl;
i++;
}
return NULL;
}
int main ()
{
pthread_t thread1_id;
ThreadParameter param1;
param1.symbol_char = 'X';
param1.count = 27;
pthread_create (&thread1_id, NULL, &print_char, ¶m1);
int i = 0;
while (i<10)
{
cout<<"O"<<endl;
i++;
}
pthread_join(thread1_id,NULL);
return 0;
}
并且它的输出没有显示 X 的预期数量。我在调用 join 函数时是否做错了什么?感谢帮助。
PS:我尝试了从 5 到 20 的各种 X 值,但它总是给我小于所需的 X 数。