Threads?

Hi guys, I have been searching and reading about threading. But it only makes me more confused.

I have 1 function which is used to insert... Let's say 10000 rows in a database.

I want to create 2 threads for the same function. So one of them insert from 1 to 5000. While the other insert from 5001 to 10000.

I found out I should use the CreateThread method?

Can anybody clarify this for me?

Maybe guide me to a solution too.

Thanks.
In this case, both threads will probably cause some contention on the database and it won't be any quicker. If you want to speed up database inserts you should consider bulk import. I don't know if your db supports it.

EDIT: There are quite a few examples of threading along with warious attempts at an explanation on this forum.
Last edited on
kbw may very well be right about this. But since you want to know about threads, here it is:

Yes, CreateThread() will give you new threads, and yes, you can create two that work using the same function. Use the parameter to tell the function the range of items to insert in the database and satisfy your curiosity. :)
Nice to know that I should be using _beginthreadex(). I didn't know. I have one DLL (a BHO) that needs revising then.
kbw, you are probably right. But I just need to test, fail and learn :)

I see in the CreateThread method. It takes some arguments which my function dont have.

I should probably edit my own function to adapt to the CreateThread method?
Whatever thread creation function you call, you need to make sure that your thread function is of the correct signature to match that function.

Good luck.
Hey- I've been referencing stuff on the web for examples/usages of threads for VC++ 6.0, thought this bit of info I stumbled on would be a big help.

http://msdn.microsoft.com/en-us/library/ms686927%28v=vs.85%29.aspx
@ Psychopete: Mutex's are more useful for example when several users are accessing the same Database (DB) on a shared network resource. If the OP were to use this method then only one thread would be writing to the DB at a time which kind of defeats the purpose of multithreading. Assuming both threads are ready to go right away.

Now if each thread had to wait for the data to come in from some stream then the performance loss from using a Mutex would not be noticable since the threads would probably not be accessing the DB at the same time. It's important when using these NOT to flood the CPU with NOP commands or file lock queries while the inactive thread is waiting for it's turn to write to the DB, this can get complicated.
Not saying that is the solution for the OP; just thought it was a good example of CreateThread. Whether or not the OP needs a mutex, the OP will have to make that assessment.
Last edited on
Topic archived. No new replies allowed.