C Windows winapi + multi-threading (HANDLE)_beginthreadex fail
This code ruined all my day. Basically I have a list of 50 webservers I administrate, I want to check them if up/alive (isAlive() function), I parse my webservers.txt file with the 50 ips/hostnames and for fastness I try to use threads (10, 20 or 30 doesn't matter) then my compiled code seems only duplicate threads with the last line in my file.
You are passing a pointer to the 'line_buffer' to each thread. However, you don't know when that thread will start. You may well have overwritten the contents of the buffer with the next line before the thread starts.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Re: C Windows winapi + multi-threading (HANDLE)_beginthreadex fail
Originally Posted by exitthematrix
An how should I safely start every thread with different lines read from my file? Because if I just do "printf("%s", line_buffer)" that works.
What do you mean with "that works"? Doing a single run and looking at the results does not tell you much in a multi-threaded world.
You should use a different buffer for each thread. That way you don't need to perform any synchronization. Seems the easiest for the use case you described.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Re: C Windows winapi + multi-threading (HANDLE)_beginthreadex fail
Originally Posted by D_Drmmr
What do you mean with "that works"? Doing a single run and looking at the results does not tell you much in a multi-threaded world.
You should use a different buffer for each thread. That way you don't need to perform any synchronization. Seems the easiest for the use case you described.
By "that works" I mean using "printf("%s", line_buffer)" that prints out every line out from my text.
( ignoring issues like possible buffer overruns, unchecked retun codes, etc ... )
alternatively, you could just pass the file path and a liner number to each thread, that in turn will open its own file stream seeking to the appropriate line.
Last edited by superbonzo; January 28th, 2013 at 08:13 AM.
Re: C Windows winapi + multi-threading (HANDLE)_beginthreadex fail
Originally Posted by exitthematrix
By "that works" I mean using "printf("%s", line_buffer)" that prints out every line out from my text.
There's little point in being vague if you want to get help. At the very least, you could post the code.
What you have now is a race condition, because you are writing (in the main thread) to the same memory you are reading from in the worker threads. That means that the results could be different on each run of your program.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Bookmarks