Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <stdio.h>
#include <pthread.h>

/* This is our thread function.  It is like main(), but for a thread */
void *threadFunc(void *arg)
{
	char *str;
	int i = 0;

	str=(char*)arg;

	while(i < 10 )
	{
		usleep(1);
		printf("threadFunc says: %s\n",str);
		++i;
	}

	return NULL;
}

int main(void)
{
	pthread_t pth;	// this is our thread identifier
	int i = 0;

	/* Create worker thread */
	pthread_create(&pth,NULL,threadFunc,"processing...");

	/* wait for our thread to finish before continuing */
	pthread_join(pth, NULL /* void ** return value could go here */);

	while(i < 10 )
	{
		usleep(1);
		printf("main() is running...\n");
		++i;
	}

	return 0;
}

i am working on Dev C++ .. on compilation it say that no such file or directory <pthread.h> .. thus the further identifiers are undeclared .. what should i do ?
Posted
Updated 1-Jan-12 22:16pm
v2

If you are working on Windows OS then POSIX threads (hence the pthread.h header) are NOT available: you have to use Win32 API threading functions (like, for instance CreateThread[^]).
 
Share this answer
 
Comments
zahra1991 2-Jan-12 4:37am    
oh ok thanx alot , i am creating thread for the time time .. i really dont have any idea regarding threads . kindly can u guide me about Win32 API threading functions ?
CPallini 2-Jan-12 4:52am    
What's better than documentation?
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686937(v=VS.85).aspx
Hi,

pthreads is an open-source POSIX thread library: POSIX Threads[^].

You can fix your compilation error by downloading the pre-compiled binaries. You would need to add the header files into your project and place the lib somewhere in your lib path.

Best Wishes,
-David Delaune
 
Share this answer
 
Comments
Albert Holguin 5-Jan-12 16:53pm    
good advice, +5
You can compile the same code in Linux machine and you don't get any compilation error for including the <pthread.h> file.

Regards,
Jauhar
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900