LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2009, 08:20 PM   #1
joey123
LQ Newbie
 
Registered: Sep 2009
Posts: 1

Rep: Reputation: 0
fopen problem in multi-thread (detach)


Hi,
I write a simple multi-thread programming. In such thread, I only open file to write a text, then end thread. When I run program, the thread is dead immediately after fopen. Sometimes, it works, sometimes, it deads. Any idea to share?

int main(int argc, char *argv[]) {
pthread_t thread1;
int *iptr1;


printf("Program Starts.\n");

iptr1 = malloc(sizeof(int));
pthread_create(&thread1, NULL, (void *) process_fopen, (void *) iptr1);
printf("main end\n");
return 0;
}

void process_fopen(void * tmp) {
int i;
FILE *fpTest2 = NULL;
char x[10]="Thread_XXX";

i = *((int *) tmp);
free(tmp);
pthread_detach(pthread_self());

printf("I'm in thread\n");
fpTest2 = fopen("./kkk.txt", "wt");
printf("After fopen\n");
if (fpTest2 == NULL) {
printf("ERROR\n");
} else {
printf("2 Confirm Open Success\n");
fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fpTest2);

}//end else
fclose(fpTest2);
printf("thread end\n");
}


the output, it shows
Program Starts.
I'm in thread

that's all, no other printf message. NO such kkk.txt file. I'm quite in doubt. This is a simple program. Do I make any mistake?

When I run this program several times, sometimes it works too. It creates kkk.txt and show those correct printf messages.

Program Starts.
I'm in thread
After fopen
2 Confirm Open Success
thread end

I'm really need some help to get me out of this.
Thanks in advance.


EDIT:
I've tried by removing
pthread_detach(pthread_self());
and put this statement in main
pthread_join(thread1, NULL);

The program works successfully. Everytime, fopen has no problem.

I guess that I'm not clear with pthread_detach(pthread_self()); concept. Any suggestion?

Last edited by joey123; 09-28-2009 at 09:05 PM. Reason: Add more information
 
Old 09-28-2009, 08:33 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
try a fopen in a program without threads to make sure your library isnt toast
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in multi thread server program naveenisback Programming 1 07-29-2009 06:55 PM
a new problem in multi thread server -client problem naveenisback Linux - Software 0 07-28-2009 08:42 AM
Multi-thread execution efficiency problem cdcshu Linux - Software 3 08-02-2007 12:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration