ページ 11

pthread_create 関数を windows でも使えるようにする

Posted: 2012年9月17日(月) 16:02
by helloworld1853
あるUnix用ソースコードを
VC++ でコンパイルできるようにしているのですが、
pthread_create で躓いています。
以下にソースコードの一部をのせました。
pthread_create は CreateThread に対応しているみたいですが、
引数がまったく違って困っています。
何かアドバイスや解決方法があれば
お願いします。

コード:

int main(void)
{
 int server_sock = -1;
 u_short port = 0;
 int client_sock = -1;
 struct sockaddr_in client_name;
 int client_name_len = sizeof(client_name);
 HANDLE newthread;

 server_sock = startup(&port);
 printf("httpd running on port %d\n", port);

 while (1)
 {
  client_sock = accept(server_sock,
                       (struct sockaddr *)&client_name,
                       &client_name_len);
  if (client_sock == -1)
   error_die("accept");
 /* accept_request(client_sock); */
 if (pthread_create(&newthread , NULL, accept_request, client_sock) != 0)
   perror("pthread_create");
 }

 closesocket(server_sock);

 return(0);
}

Re: pthread_create 関数を windows でも使えるようにする

Posted: 2012年9月17日(月) 17:30
by たかぎ
pthreadはMinGWにも移植されているので、そのソースを参考にしては?
いっそのことMinGWを使うというのも一つの手です。

Re: pthread_create 関数を windows でも使えるようにする

Posted: 2012年9月18日(火) 09:37
by softya(ソフト屋)
前回の質問が未解決ですのでお願いします。
「きれいなソースコードの書き方 • C言語交流フォーラム ~ mixC++ ~」
http://dixq.net/forum/viewtopic.php?f=3&t=11189#p92586

それと、CreateThreadが使えるならpthread_createは何ら難しい部分はないはずです。
「Man page of PTHREAD_CREATE」
http://linuxjm.sourceforge.jp/html/glib ... ate.3.html

CreateThreadに関してはC言語の標準ライブラリを使う場合は代わりに_beginthreadexを使ってください。

Re: pthread_create 関数を windows でも使えるようにする

Posted: 2012年9月20日(木) 23:43
by helloworld1853
ありがとうございます。
これですべてのソースコードを
windows用へ変換できました。

>前回の質問が未解決ですのでお願いします。
ご心配してくださってありがとうございます。
寄り道してしまいました。

コード:

CreateThread(0, 0, (LPTHREAD_START_ROUTINE)accept_request, (LPVOID)client_sock, 0, (LPDWORD)&newthread
たかぎさん、softyaさん
ありがとうございます。

Re: pthread_create 関数を windows でも使えるようにする

Posted: 2012年9月21日(金) 16:29
by たかぎ
softyaさんも書かれていますが、CreateThreadではなく_beginthreadまたは_beginthreadexを使ってください。
元がUnixをターゲットにしているソースであれば、標準ライブラリを排除することはまず無理でしょう。