-1

プログラムはコマンド ラインから引数を取得し、posix スレッドを介して引数を追加する必要があります。しかし、Xcode は正常にビルドしますが、何も出力しません。このコードに何か問題がありますか。ありがとう

#include <iostream>
#include <pthread.h>

using namespace std;

void *Add(void *threadid){
  long tid;
  tid =(long)threadid;
  long sum=0;
  sum=sum+tid;
  printf("%ld.\n",sum);
  pthread_exit(NULL);
}    

void *Print(void *threadid){
  long tid;
  tid =(long)threadid;

  printf("%ld.\n",tid);
  pthread_exit(NULL);
}   

int main (int argc, char const *argv[])
{
  if(argc<6){
    printf("you need more arguments");
    return -1;
  }

  long real[5];
  pthread_t athread,bthread;

  for (int x=1;x<=5;x++)
    real[x-1]=atol(argv[x]);

  for(int y=1;y<=5;y++)
    pthread_create(athread[y],NULL,Add,(void *)&real[y]);

  for(int y=1;y<=5;y++)
    pthread_create(bthread[y],NULL,Print,(void *)&real[y]);

  pthread_exit(NULL);
  return 0;
}
4

2 に答える 2

1

まず、pthread_createメソッドが成功したかどうかを確認する必要があると思います。私はAppleの下でpthreadの経験がありませんが、そのコードに基づいて、スレッドの作成に問題があると思います。

于 2012-05-29T18:12:08.563 に答える
1

まず、printfは で定義されており、 では定義されstdio.hていませんiostream。を使用して C++ の方法で実行したい場合は、代わりiostreamに thencout << "Blabla " << var << endl;を使用する必要があります。

pthread_create次に、間違った引数で呼び出しています。定義どおりであり、配列ではathreadありbthreadませんが、配列として使用します。が最初の引数としてpthread_create期待されており、あなたが提供しているため、なぜこれがコンパイルされるのか完全にはわかりません。コードがコンパイルされると、実行時にクラッシュする可能性が高くなります。pthread_t**pthread_t

第 3 に、加算スレッドに参加していません。これは、加算スレッドが終了する前に印刷スレッドが開始される可能性があることを意味します。

第 4 に、ローカル変数に合計しています。グローバルなものに合計することになっています。ミューテックスなどでアクセスを保護することを忘れないでください。

第 5 に、スレッド ルーチンへの引数が間違っています。値自体ではなく値へのポインターを渡し、後でポインターを値自体として再解釈します。(void *)real[y]ほとんどの場合、 ではなくを使用することをお勧めします(void *)&real[y]。へのキャストは、すべてのシステムで機能longするvoid *わけではないことに注意してください。Mac OS X では、longとの両方void *が同じ長さ (32 ビットまたは 64 ビット) ですが、これは一般的に当てはまりません。

編集: あなたのコードは OS X でもコンパイルされません:

$ g++ -o t.x t.cpp
t.cpp: In function ‘int main(int, const char**)’:
t.cpp:37: error: cannot convert ‘_opaque_pthread_t’ to ‘_opaque_pthread_t**’ for argument ‘1’ to ‘int pthread_create(_opaque_pthread_t**, const pthread_attr_t*, void* (*)(void*), void*)’
t.cpp:40: error: cannot convert ‘_opaque_pthread_t’ to ‘_opaque_pthread_t**’ for argument ‘1’ to ‘int pthread_create(_opaque_pthread_t**, const pthread_attr_t*, void* (*)(void*), void*)’

$ clang -o t.x t.cpp
t.cpp:37:5: error: no matching function for call to 'pthread_create'
    pthread_create(athread[y],NULL,Add,(void *)&real[y]);
    ^~~~~~~~~~~~~~
/usr/include/pthread.h:304:11: note: candidate function not viable: no known
      conversion from 'struct _opaque_pthread_t' to 'pthread_t *' (aka
      '_opaque_pthread_t **') for 1st argument;
int       pthread_create(pthread_t * __restrict,
          ^
t.cpp:40:5: error: no matching function for call to 'pthread_create'
    pthread_create(bthread[y],NULL,Print,(void *)&real[y]);
    ^~~~~~~~~~~~~~
/usr/include/pthread.h:304:11: note: candidate function not viable: no known
      conversion from 'struct _opaque_pthread_t' to 'pthread_t *' (aka
      '_opaque_pthread_t **') for 1st argument;
int       pthread_create(pthread_t * __restrict,
          ^
2 errors generated.

XCode が提供しているエラー メッセージも表示されませんか?

于 2012-05-29T21:04:43.597 に答える