Mingw's failure to compile multi-thread programs and Solutions

Source: Internet
Author: User

In the linux environment, I learned the C language through gedit + gcc for a period of time and found myself more and more admired for linux.
You can develop code without using IDE. I couldn't do it, so I went back to windows.

Mingw is a gnu c/c ++ tool set for windows. It mainly includes the compiler gcc, the connector make, and the Debugger gdb.
Eclipse is a very useful Java IDE. CDT is short for C/C ++ Development Tools. It is a plug-in that enables Eclipse to support C/C ++ Development.
My development environment is as follows:
Win7 + eclipse + MinGW + CDT

Purpose:
1) Use eclipse and MingW to build a Windows c/c ++ Development Environment
2) compiled and tuned a multi-threaded Program
Installation of eclipse and MingW is not important here.

1. Configure the C/C ++ Development Environment
Right-click my computer and choose Properties> advanced> environment variable. Then:
1) add C: \ MinGW \ bin to the PATH
2) create a new LIBRARY_PATH variable with the value set to C: \ MinGW \ lib, which is the location of the standard library.
3) Create the C_INCLUDE_PATH variable with the value set to C: \ MinGW \ include.
4) create a CPLUS_INCLUDE_PATH variable with the value 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. Set eclipse Environment Variables
Eclipse Project-> Properties-> C/C ++ Build-> Environment to view the corresponding PATH variable
It mainly depends on whether a PATH variable containing the bin directory of MinGW is added. This variable is generally automatically added when a project is created.
However, if the PATH variable is not correctly set, an error is reported during compilation, indicating that g ++ or gcc cannot be run.

3. Set a multi-threaded Dynamic Link Library
Project-> Properties-> C/C ++ Build-> Settings-> Tool Settings-> gcc c ++ Linker-> Libraries
Add Libraries (-l): lpthread

4. Example of a simple Thread Pool

# 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 );
}

// The following is the test code.

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. Problems and Solutions
1) "Error launching builder (make-k all)" appears during project build )"
Run the following command under C: \ MinGW \ bin:
Mingw32-make.exeis more named make.exe, because it defaults to make this file name in the system rather than the mingw32-make when using Eclipse.

2) "launch failed, binary not found" appears during project build"
The error is reported because no compiler is specified. To enable CDT to use MinGW for compilation, you need to set it in Eclipse.
Method 1: go to Window> Preferences> C/C ++> New CDT project wizard> Makefile Project and find Binary Parser to cancel Elf Parser and select PE Windows Parser. Or other compilers, depending on the specific situation,
You can also specify gnu elf Parser.
Method 2: Go to projrdbms-> properties-> c/c ++ build-> settings-> Binary Parser to set it, for example, use GNU Elf Parser

3) undefined reference to 'pthread _ create'
Undefined reference to 'pthread _ join'
Cause:
The pthread library is not the default library of the Linux system. Because Mingw is not included, it is useless to set a multi-thread dynamic link library in the project.
Ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-8-0-release.exe
Open pthreads-w32-8-0-release.exe and rename libpthreadGC2.a in Pre-built.2 to libpthread. a to the c: \ mingw \ lib directory,
Copy pthread. h to the c: \ mingw \ include directory.

You are welcome to repost this article.
Author: meteor
Source: http://blog.sina.com.cn/staratsky

This article is from the "Meteor blog" blog, please be sure to keep this source http://staratsky.blog.51cto.com/2513234/1144703

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.