|
Hello. I have got couple of basic questions about thread. I request if they are too simple, please excuse me.
1 - By default, every thread has one megabyte of stack space. Therefore, you can create at most 2,048 threads ( copied From MSDN ). How do I maximize or minimize stack size and how do I calculate max number of threads for this stack size.
2 - Lets say I create 10 threads using CreateThread() and each of them returns a valid handle. What is the best technique to store all these handles and then use them to close/terminate those threads.
Thanks for sharing your thoughts on this one.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
AmbiguousName wrote: How do I maximize or minimize stack size and how do I calculate max number of threads for this stack size.
Why? If you try to create that many threads you are probably going to fail. More would be worse.
AmbiguousName wrote: What is the best technique to store all these handles and then use them to close/terminate those threads.
That is a little bit imprecise but presumably you are asking about a thread pool.
|
|
|
|
|
1. CreateThread lets you set the stack size. to maximize, set it to an ungodly huge number. to minimize, set it to zero. either way, the thread won't run.
i guess i don't understand the question.
2. create a std::vector<HANDLE> array, populate it with your HANDLEs. when you're done, run through it and close the threads. but, yes, it sounds like what you want is a thread pool. best to find one that's already written, since they can be quite challenging to write from scratch.
|
|
|
|
|
Chris Losinger wrote: to maximize, set it to an ungodly huge number. to minimize, set it to zero. either way, the thread won't run.
Always on the lookout to learn something new...Why do you say the thread won't run? I can understand it won't run if it has no stack space, but what about a large stack space?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
AmbiguousName wrote: 1 - By default, every thread has one megabyte of stack space. Therefore, you can create at most 2,048 threads ( copied From MSDN ). How do I maximize or minimize stack size and how do I calculate max number of threads for this stack size.
Don't think it's beneficial to even be considering changing the maximum number of threads. If you have to be concerned about that, then your process design is probably not well thought out.
AmbiguousName wrote: 2 - Lets say I create 10 threads using CreateThread() and each of them returns a valid handle. What is the best technique to store all these handles and then use them to close/terminate those threads.
You can store the handles any way you want (simple array or any container). Look at the WaitForSingleObject() (and corresponding WaitForMultipleObjects()) call for tracking when a thread has finished his task. You can also use signaling... such as events for tracking when the threads are finishing up doing their work.
Additionally, there's a concept for managing multiple worker threads without the overhead of constantly restarting them called a "thread pool". In certain cases, that can be beneficial since the overhead of starting threads can be rather significant.
|
|
|
|
|
How do I calculate number of threads for a particular stack size (2048 for 1MB) ?
This world is going to explode due to international politics, SOON.
|
|
|
|
|
Its too slow when I open -> write -> close a file (see code below).
Are they a way of opening the file and always writing at the top of the file; e.g. I do not want to Append text, just write the same 100 lines of text each time at the top of the file.
#include <stdio.h>
FILE * pFile;
if ((pFile = fopen("myfile.txt", "w")) == NULL)
{
}
else
{
fclose(pFile);
}
Waiting in hope!
grahamfff
|
|
|
|
|
No, there is no way, since all the data that you write would overwrite whatever is already there. You would need to hold the data in memory, and rewrite all of it every time.
Veni, vidi, abiit domum
|
|
|
|
|
I just want to append to the TOP of the file each time. Its too slow doing an open -> write -> close.
In a 20 msec loop and sometimes two files need to hav data written to them.
No timing problems with the append, but this could result in large files if I do not overwirie.
grahamfff
|
|
|
|
|
Grahamfff wrote: I just want to append to the TOP of the file each time. As I said before, you cannot do it; the system just does not work that way. Perhaps it would be better if you explained why you want to do this.
Veni, vidi, abiit domum
|
|
|
|
|
In a 20 msec loop and sometimes two files need to have data written to them and the open -> write -> close takes about 8-10 msec each and sometimes get an overrun.
grahamfff
|
|
|
|
|
Then you need to work on your system design; you cannot change the laws of physics.
Veni, vidi, abiit domum
|
|
|
|
|
As Eugen alludes to, a time-critical loop is not the place to muck about. Write the data to the end of the file then deal with the order later on.
Furthermore, I assume that you're dealing with different files each time through the loop. If not, then again, don't muck about. Open the file before the loop and close it again afterwards.
|
|
|
|
|
// I do not want to Append text, just write the same 100 lines of text each time at the top of the file.
Any appending in the file context is more rapid than an inserting.
Of course, you could try to append the reversed content to be read later as reversed as well...
They sought it with thimbles, they sought it with care;
They pursued it with forks and hope;
They threatened its life with a railway-share;
They charmed it with smiles and soap.
|
|
|
|
|
Grahamfff wrote: Are they a way of opening the file and always writing at the top of the file; Do you mean having the newest data at the beginning of the file (rather than at the end)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Yes that would do!
They are recoverery data files, so periodically writing them. Just interested in the newest data.
grahamfff
|
|
|
|
|
Grahamfff wrote: They are recoverery data files,
Now, finally, you are providing some useful info.
First off, the file system is a bottleneck. The only way to speed up file access is use a different file system. But you probably don't have that choice.
Second, repeatedly writing to hard disk in a fixed time loop every 20ms is an incredibly stupid idea, for various reasons - this is simply not the intended purpose of a file system! You should consider different storage media, depending an what is available to you, or you should consider to just wait longer between writes. I'm not an expert on this, but Flash drives, SRAM, or even USB may be more suitable. (Although writing to flash drives 50 times per second is a sure way to destroy them in short order...)
Third, I can't think of any reason to write recovery info more often then once every couple of seconds: a recovery feature usually means the ability to restore a state that is difficult to reproduce, either because it is the result of interacting with a non-deterministic system (such as a human), or because there is no other way to reproduce the data (e. g. in very long range communication such as talking to mars rovers and the like). Either way, losing the data from as little a timespan as one second is meaningless! OTOH, if for whatever incomprehensible reason you really, really need that info stored every 0.02s flawlessly, then the file system simply is too slow to reliably do this - not to mention that trying it will slow your entire application to a crawl. As suggested above, use a different storage medium!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
|
I want to make an windows 8.1 store app(can be desktop but prefer store).
I know the following languages :
visual basic 2012(win rt)(mostly preferable)
visual c++( not so good)
i have to make the beta fast
PROGRAM:-
->a interactive full screen homepage with 3 buttons and settings in the charm bar
HOME PAGE buttons:-
->1st button: record screen: records the desktop and other win 8 apps with webcam support and audio from mic
->2nd button: Edit video: more like mixing. you can drag-drop video to timeline(like windows movie maker) and 2 channel audio(one for voice and one for song)
->3rd button: upload video: push the video to the web as mp4 or avi at youtube or facebook or save them to disk
VOICE RECOGNITION:-
(optional) at the home screen when user say some command like 'program run record' or 'program edit video', the program does the same.
i want some leads...i have no experience in intensive, professional programming but i want to do so.
i would really appreciate if you tell me what i have to do,
specifically:
things that are easy,
things that need some stress
and work on!
Thanks in advance
|
|
|
|
|
I would suggest you go to the articles section here[^] and look for articles on the subjects that you need to learn. You can also use Google for wider searches.
Veni, vidi, abiit domum
|
|
|
|
|
tell your boss to hire someone who can.
|
|
|
|
|
I had compiled MFC Grid control 2.27[^] project, but I get a lot of warnings (I use VC6):
GridCell.cpp
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\xmemory(39) : warning C4100: '_P' : unreferenced formal parameter
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\xmemory(41) : warning C4100: '_P' : unreferenced formal parameter
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\vector(249) : warning C4663: C++ language change: to explicitly specialize class template 'vector' use the following syntax:
template<> class vector<bool,class std::allocator<unsigned int> > ...
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\vector(156) : warning C4018: '<' : signed/unsigned mismatch
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\vector(156) : while compiling class-template member function 'void __thiscall std::vector<unsigned int,class std::allocator<unsigned int> >::insert(unsigned int *,unsigned int,const unsig
ned int &)'
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\vector(167) : warning C4018: '<' : signed/unsigned mismatch
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\vector(156) : while compiling class-template member function 'void __thiscall std::vector<unsigned int,class std::allocator<unsigned int> >::insert(unsigned int *,unsigned int,const unsig
ned int &)'
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\xmemory(37) : warning C4100: '_P' : unreferenced formal parameter
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\xmemory(68) : see reference to function template instantiation 'void __cdecl std::_Destroy(unsigned int *)' being compiled
and so on ... I had tried
#pragma warning(disable 4100) ... doesn't have any affect ...
How can I get rid of this warnings ? Thank you.
|
|
|
|
|
Looking at thos messages suggests that you are using a very old version of Visual Studio. You probably need to upgrade to a later version.
Veni, vidi, abiit domum
|
|
|
|
|
You could either set the warning level for the whole project to 3 in the settings.
Or you can include the line
#pragma warning(disable 4100)
in you Stdafx.h file before the relevant includes.
Or if you don't want to do this, you can wrap the MFC includes in the following:
#pragma warning (push, 3)
#include "whatever"
#pragma warning (pop)
The good thing about pessimism is, that you are always either right or pleasently surprised.
|
|
|
|
|
I had tried to put
#pragma warning(disable 4100)
in stdafx.h, but doesn't work ... I'll try other one ...
|
|
|
|