■掲示板に戻る■ 1- 101- 201- 301- 401- 501- 601- 701- 801- 901- 1001- 最新50



レス数が1000を超えています。残念ながら全部は表示しません。

OpenBSDユーザーコーナー

732 :名無しさん@お腹いっぱい。 :02/07/07 15:12
ねぇねぇ、下のソースが動かないんだけど。。。
どこがだめなのかなぁ??
OpenBSD 3.0です。Linuxでは動作しました。
pthread_detach()は無いとか言われちゃうし。。。
はぁ、NetBSDにしようかなぁ・・・。

typedef struct ___thread_id{
  pthread_t tid; int     flag;
} THREAD_TID;
typedef void * (*THREAD_ENTRY_FUNC)(void *);
typedef void * THREAD_ENTRY_FUNC_ARGU;
typedef pthread_mutex_t THREAD_CRITICAL;
enum thread_attr { THREAD_DETACH, THREAD_NORMAL };
static long now_thread, max_thread = 100;
void thread_max(int max) {
  max_thread = max;
}
int thread_create(THREAD_TID * tid, THREAD_ENTRY_FUNC func,
       THREAD_ENTRY_FUNC_ARGU arg, enum thread_attr flag ) {
  unsigned int t;
  if (max_thread <= now_thread ) return 1;
  tid->flag = flag;
  if (flag == THREAD_DETACH) {
    pthread_attr_t tattr;
    if (pthread_attr_init(&tattr)) return 1;
    if (pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_DETACHED)) return 1;
    t = pthread_create(&(tid->tid), &tattr, func, arg);
    pthread_attr_destroy(&tattr);
  }
  else
    t = pthread_create(&(tid->tid), NULL, func, arg);
  if (t != 0) return 1;
  now_thread++;   return 0;
}
void thread_exit(int res) {
  now_thread--; pthread_exit((void *)&res);
}
int thread_close(THREAD_TID * tid) {
  if (tid->flag == THREAD_DETACH) return 1;
  pthread_join(tid->tid, NULL); tid->flag = THREAD_DETACH;
  return 0;
}
void * mythread(void *ptr) {
  int i, tid = (int)ptr;
  for (i = 0; i < 50; i++)
    printf("[%d] %d\n", tid, i);
  sleep(10); thread_exit(0); return 0;
}
int main(void) {
  int i;
  THREAD_TID tid[50];
  thread_max(50);
  for (i = 0; i < 50; i++)
    if (thread_create(&(tid[i]), mythread, (void *)i, THREAD_DETACH) ) {
      printf("Error!!!(%d)\n", i); exit(1);
    }
  sleep(10); return 0;
}


次100 最新50

read.cgi ver5.26+ (01/10/21-)