程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> mingw無法編譯多線程程序的問題及解決

mingw無法編譯多線程程序的問題及解決

編輯:關於C語言

在linux環境下,用gedit+gcc方式學習一段時間的c語言,發現自己越來越佩服linux的牛人了。
居然不用IDE也能開發代碼。我做不到,所以有回到了windows的懷抱。

mingw是windows版本的gnu c/c++工具集 主要包括編譯器gcc,連接器make,調試器gdb
Eclipse是很好用的開發Java的IDE,CDT的全稱是C/C++ Development Tools,是使Eclipse能夠支持C/C++開發的插件。
本人使用的開發環境如下:
win7+eclipse +MinGW + CDT

本文的目的:
1)使用eclipse、MingW獨構建Windows版的c/c++開發環境
2)編譯並調通了一個多線程的程序
eclipse、MingW的安裝此處不做重點。

1、配置C/C++開發環境
右擊我的電腦,點屬性->高級->環境變量。然後:
1)在PATH裡加入C:\MinGW\bin
2)新建LIBRARY_PATH變量,值設為C:\MinGW\lib,這是標准庫的位置。
3)新建C_INCLUDE_PATH變量,值設為C:\MinGW\include。
4)新建CPLUS_INCLUDE_PATH變量,值為C:\MinGW\include\c++\3.4.2;C:\MinGW\include\c++\3.4.2\mingw32;C:\MinGW\include\c++\3.4.2\backward;C:\MinGW\include

2、設置eclipse的環境變量
Eclipse的Project->Properties->C/C++ Build->Environment中去查看對應的PATH變量
主要是看有沒有添加一個包含MinGW的bin目錄的PATH變量。這個變量在創建工程的時候一般會自動添加。
但如果沒有正確設置PATH變量,編譯的時候就會報錯說不能運行g++或者gcc。

3、設置多線程動態鏈接庫
Project->Properties->C/C++Build->Settings->Tool Settings->GCC C++ Linker->Libraries
添加Libraries (-l): lpthread

4、一個簡單的線程池的例子

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <assert.h>

typedef struct worker
{
void *(*process) (void *arg);
void *arg;
struct worker *next;

} CThread_worker;

typedef struct
{
pthread_mutex_t queue_lock;
pthread_cond_t queue_ready;

CThread_worker *queue_head;

int shutdown;
pthread_t *threadid;
int max_thread_num;
int cur_queue_size;

} CThread_pool;

int pool_add_worker (void *(*process) (void *arg), void *arg);
void *thread_routine (void *arg);

static CThread_pool *pool = NULL;
void
pool_init (int max_thread_num)
{
pool = (CThread_pool *) malloc (sizeof (CThread_pool));

pthread_mutex_init (&(pool->queue_lock), NULL);
pthread_cond_init (&(pool->queue_ready), NULL);

pool->queue_head = NULL;

pool->max_thread_num = max_thread_num;
pool->cur_queue_size = 0;

pool->shutdown = 0;

pool->threadid =
(pthread_t *) malloc (max_thread_num * sizeof (pthread_t));
int i = 0;
for (i = 0; i < max_thread_num; i++)
{
pthread_create (&(pool->threadid[i]), NULL, thread_routine,
NULL);
}
}

int
pool_add_worker (void *(*process) (void *arg), void *arg)
{
CThread_worker *newworker =
(CThread_worker *) malloc (sizeof (CThread_worker));
newworker->process = process;
newworker->arg = arg;
newworker->next = NULL;

pthread_mutex_lock (&(pool->queue_lock));
CThread_worker *member = pool->queue_head;
if (member != NULL)
{
while (member->next != NULL)
member = member->next;
member->next = newworker;
}
else
{
pool->queue_head = newworker;
}

assert (pool->queue_head != NULL);

pool->cur_queue_size++;
pthread_mutex_unlock (&(pool->queue_lock));
pthread_cond_signal (&(pool->queue_ready));
return 0;
}

int
pool_destroy ()
{
if (pool->shutdown)
return -1;
pool->shutdown = 1;

pthread_cond_broadcast (&(pool->queue_ready));

int i;
for (i = 0; i < pool->max_thread_num; i++)
pthread_join (pool->threadid[i], NULL);
free (pool->threadid);

CThread_worker *head = NULL;
while (pool->queue_head != NULL)
{
head = pool->queue_head;
pool->queue_head = pool->queue_head->next;
free (head);
}
pthread_mutex_destroy(&(pool->queue_lock));
pthread_cond_destroy(&(pool->queue_ready));

free (pool);
pool=NULL;
return 0;
}

void * thread_routine (void *arg)
{
printf ("starting thread 0x%x\n", pthread_self ());
while (1)
{
pthread_mutex_lock (&(pool->queue_lock));
while (pool->cur_queue_size == 0 && !pool->shutdown)
{
printf ("thread 0x%x is waiting\n", pthread_self ());
pthread_cond_wait (&(pool->queue_ready), &(pool->queue_lock));
}

if (pool->shutdown)
{
pthread_mutex_unlock (&(pool->queue_lock));
printf ("thread 0x%x will exit\n", pthread_self ());
pthread_exit (NULL);
}

printf ("thread 0x%x is starting to work\n", pthread_self ());

assert (pool->cur_queue_size != 0);
assert (pool->queue_head != NULL);

pool->cur_queue_size--;
CThread_worker *worker = pool->queue_head;
pool->queue_head = worker->next;
pthread_mutex_unlock (&(pool->queue_lock));

(*(worker->process)) (worker->arg);
free (worker);
worker = NULL;
}
pthread_exit (NULL);
}

// 下面是測試代碼

void * myprocess (void *arg)
{
printf ("threadid is 0x%x, working on task %d\n", pthread_self (),*(int *) arg);
_sleep (1);
return NULL;
}

int main (int argc, char **argv)
{
pool_init (3);

int *workingnum = (int *) malloc (sizeof (int) * 10);
int i;
for (i = 0; i < 10; i++)
{
workingnum[i] = i;
pool_add_worker (myprocess, &workingnum[i]);
}
_sleep (5);
pool_destroy ();

free (workingnum);
return 0;
}

5、問題及解決方法
1)build project時出現“Error launching builder (make -k all )”
先將C:\MinGW\bin底下的
mingw32-make.exe更名為make.exe,因為在Eclipse使用時它預設會抓系統裡make這個文件名而不是mingw32-make.

2)build project時出現“launch failed,binary not found”
報錯是因為沒有指定編譯器。為了讓CDT能夠采用MinGW進行編譯,需要在 Eclipse 中進行設定
方法一:去Window->Preferences->C/C++->New CDT project wizard->Makefile Project下,找到 Binary Parser 取消 Elf Parser 改選 PE Windows Parser。或是其他編譯器,看具體情況而定,
當然你也可以指定GNU ELF Parser。
方法二:去projrct -> properties -> c/c++ build -> settings ->Binary Parser下設置一下,比如使用GNU Elf Parser

3)undefined reference to 'pthread_create'
undefined reference to 'pthread_join'
問題原因:
pthread 庫不是 Linux 系統默認的庫,由於Mingw下沒有帶,所以在項目中設置多線程動態鏈接庫,也不管用。
ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-8-0-release.exe
解開pthreads-w32-2-8-0-release.exe,把Pre- built.2中的libpthreadGC2.a改名為libpthread.a復制到c:\mingw\lib目錄,
pthread.h復制到c: \mingw\include目錄即可解決

本文歡迎轉載,轉載請注明作者與出處
作者:流星
出處:http://blog.sina.com.cn/staratsky

本文出自 “流星的博客” 博客,請務必保留此出處http://staratsky.blog.51cto.com/2513234/1144703

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved