trying to get a feel for pthreads and multithreaded programming in C. Ive managed, thanks to this site, to get the library linked so that it no longer gives me errors in the compiling.
Im trying to get 2 threads running, one that prints 1000000 x's and one that prints 1000000 o's.
There is a problem, however, when running the program. The command line pops up for a millisecond and then dies, nothing seems to happen. Not even any compile errors or anything that i can then fix. If i comment out the thread creation and thread join statements then the command line pops up and waits for a key to be hit because of the system("PAUSE").
here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <pthread.h>
void* printChar(void *c);
int main(int argc, char *argv[])
{
pthread_t thread1;
pthread_t thread2;
pthread_create(&thread1,NULL,printChar,"x");
pthread_create(&thread2,NULL,printChar,"o");
pthread_join(thread1,NULL);
system("PAUSE");
return 0;
}
void* printChar(void *c)
{
char *str;
str = (char*)c;
int i;
for(i = 0; i < 1000000; i++)
{
printf("%c",str);
}
}
I'm running 开发者_如何学编程a DOS C-application from the Bloodshed Dev-C++ IDE. My version of the pthread .dll and .a files are: libpthreadGC2.a and pthreadGC2.dll
If you need any other specs lemmie know
thanks in advance!
Take a look at your printf: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’. So it should be printf("%s",str);
精彩评论