|
Hi,
This is very basic computer science problem. Look at your code cafefully.
First read all about context switching[^] and once you completely understand look over your code again.
Now ask yourself what would happen if ThreadA context switches in the middle of drawrect() and ThreadB continues execution of drawrect().
Best Wishes,
-David Delaune
|
|
|
|
|
David,
My reading of that is that it should, in theory, work. The variables used by each function are all stack variables and each thread has its own stack, so context switching should not affect it. I can only assume that there is some code in the actual library that is not thread safe.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
David,
Thanks for the reply, I've read about context switching and now I have so many question going on my head.
First thing first, what actually happen when you multithread? Does the processor run two or more processes independently at the same time , or does it still need to switch between processes?
because if it does run the two separately then why would it need to switch context?
another one is if it does run the two separately, then i'm guessing the function drawrect() can't be used at the same time and thus needs to be switched between processes. by logic, will it work if i apply semaphores to it?
So there you go. Can you help me with this? or did i confuse you with my questions?
|
|
|
|
|
Hi,
yudhistira dewanata wrote: First thing first, what actually happen when you multithread? Does the processor run two or more processes independently at the same time , or does it still need to switch between processes?
because if it does run the two separately then why would it need to switch context?
You would obviously need more physical cpu registers to execute processes completely independently if we define 'independently' as 'single task and never subject to context switches on the physical hardware'. Each operating system internally implements this in its own way[^].
I like to visualize engineering problems. As you are looking at my forum post... you can visualize your web browser application as being a node on a tree structure. The node at the very top is the probably the kernel and where the scheduling occurs. Multitasking [^] can be implemented in many ways.
yudhistira dewanata wrote: i'm guessing the function drawrect() can't be used at the same time and thus needs to be switched between processes. by logic, will it work if i apply semaphores to it?
No, because the function setcolor() is changing a global variable.
In fact I read your code before I read your question and I immediately saw the error. It is because I actually used these libraries many years ago.
Best Wishes,
-David Delaune
|
|
|
|
|
Ohh... I see...
Now i finally get it! Thanks a lot for the very good reply.
|
|
|
|
|
I am working on a project about data mining. my company has given me 6 million dummy customer info of twitter. I was assigned to find out the similarity between any two users. can anyone could give me some ideas how to deal with the large community data? Thanks in advance
Problem : I use the tweets & hashtag info(hashtags are those words highlighted by user) as the two criteria to measure the similarity between two different users. Since the large number of users, and especially there may be millions of hastags & tweets of each user. Can anyone tell me a good way to fast calculate the similarity between two users? I have tried to use FT-IDF to calculate the similarity between two different users, but it seems infeasible. can anyone have a very super algorithm or good ideas which could make me fast find all the similarities between users?
For example:
user A's hashtag = {cat, bull, cow, chicken, duck}
user B's hashtag ={cat, chicken, cloth}
user C's hashtag = {lenovo, Hp, Sony}
clearly, C has no relation with A, so it is not necessary to calculate the similarity to waste time, we may filter out all those unrelated user first before calculate the similarity. in fact, more than 90% of the total users are unrelated with a particular user. How to use hashtag as criteria to fast find those potential similar user group of A? is this a good idea? or we just directly calculate the relative similarity between A and all other users? what algorithm would be the fastest and customized algorithm for the problem?
|
|
|
|
|
|
Hello Friends
I am creating a MFC application in which adding Scrollbar to CDialogBar. But its not showing automatically when i open a file which displays some boxes in CDialog. When I click on scroll bar then only its showing up otherwise not.
Here am using like this to display scrollbar.
SetScrollRange(SB_VERT,0,4,TRUE);
SetScrollPos(SB_VERT,0);
ShowScrollBar(SB_VERT);
Thanks & Regards
Yogesh Sikri
|
|
|
|
|
Hello Mr. Sikri,
Please refer to the following:
http://msdn.microsoft.com/en-us/library/zz1h061h%28v=vs.80%29.aspx
You may find it useful to you problem.
Hope this helps!
Best of Luck!
With Kind Regards,
April
Comm100 - Leading Live Chat Software Provider
modified 27-May-14 8:34am.
|
|
|
|
|
When pasting links into answers please make sure they are clickable, by using the "link" or "[^]" controls above the edit box.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hello everybody,
Does anybody know how to put commands in a ribbon markup file (for example a ribbon.xml )?
Here's the code:
<?xml version='1.0' encoding='utf-8'?>
<Application xmlns='http://schemas.microsoft.com/windows/2009/Ribbon'>
<!-- Commands section -->
<Application.Commands>
<Command Name="cmdTab1"
Comment="The main tab of the ribbon."
Symbol="cmdTab1"
Id="500" >
<Command.LabelTitle>
<String Id ="501">Home</String>
</Command.LabelTitle>
</Command>
<Command Name="cmdButtonKillUapps"
Comment="Kill all user mode applications on the computer."
Symbol="cmdButtonKillUapps"
Id="11111" >
<Command.LabelTitle>
<String Id ="1214">Kill U. Applications</String>
</Command.LabelTitle>
<Command.LargeImages>
<Image Id="7097">res/kuAppsBlue.bmp</Image>
</Command.LargeImages>
<Command.SmallImages>
<Image Id="9802">res/kuAppsBlue16x16.bmp</Image>
</Command.SmallImages>
</Command>
<Command Name="cmdButtonKillKapps"
Comment="Kill all kernel mode applications on the computer."
Symbol="cmdButtonKillKapps"
Id="2222" >
<Command.LabelTitle>
<String Id ="5498">Kill K. Applications</String>
</Command.LabelTitle>
<Command.LargeImages>
<Image Id="2322">res/kuAppsBlue.bmp</Image>
</Command.LargeImages>
<Command.SmallImages>
<Image Id="1047">res/kuAppsBlue16x16.bmp</Image>
</Command.SmallImages>
</Command>
<Command Name="cmdButtonBlnkScrn"
Comment="Lock up and out computer of people on client(s)."
Symbol="cmdButtonBlnkScrn"
Id="502" >
<Command.LabelTitle>
<String Id ="503">Blank Screen</String>
</Command.LabelTitle>
<Command.LargeImages>
<Image Id="504">res/bscreenBlue_ico.bmp</Image>
</Command.LargeImages>
<Command.SmallImages>
<Image Id="505">res/bscreenBlue_ico16x16.bmp</Image>
</Command.SmallImages>
</Command>
<Command Name="cmdButtonDisaIn"
Comment="Turn off mice(s) and keyboard(s) as if the devices were unplugged from the computer."
Symbol="cmdButtonDisaIn"
Id="599" >
<Command.LabelTitle>
<String Id ="510">Disable Input</String>
</Command.LabelTitle>
<Command.LargeImages>
<Image Id="519">res/key_blanklock.bmp</Image>
</Command.LargeImages>
<Command.SmallImages>
<Image Id="554">res/key_blanklock16x16.bmp</Image>
</Command.SmallImages>
</Command>
</Application.Commands>
<Application.Views>
<Ribbon>
<Ribbon.Tabs>
<Tab CommandName="cmdTab1">
<Group CommandName="cmdGroup1" SizeDefinition="ThreeButtons">
<SplitButton CommandName="cmdButtonKillUapps">
<Button CommandName="cmdButtonKillKapps" />
</SplitButton>
<ToggleButton CommandName="cmdButtonBlnkScrn" />
<ToggleButton CommandName="cmdButtonDisaIn" />
</Group>
</Tab>
</Ribbon.Tabs>
</Ribbon>
</Application.Views>
</Application>
Like when I hover my cursor with my mice over a command it should show a box showing a title and a concise description of what it basically does.
I am using the Windows 7 S.D.K. with Visual Studio 2010 and comping this through an external executable called uicc.exe .
Simple Thanks and Regards,
Brandon T. H.
Programming in C and C++ now, now developing applications, services and drivers (and maybe some kernel modules...psst kernel-mode drivers...psst).
Many of life's failures are people who did not realize how close they were to success when they gave up. - Thomas Edison
modified 27-Dec-12 20:48pm.
|
|
|
|
|
|
Yes, exactly what I was looking for, but how do you add return/enter spaces?, in C++ in a console application it is \n .
Simple Thanks and Regards,
Brandon T. H.
Programming in C and C++ now, now developing applications, services and drivers (and maybe some kernel modules...psst kernel-mode drivers...psst).
Many of life's failures are people who did not realize how close they were to success when they gave up. - Thomas Edison
|
|
|
|
|
Follow the "See Also" link.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Sorry, fat fingers, it just got posted before I fixed the formating!
<pre lang="text">
Could someone please explain this or give me some reference?
I am processing WM_DEVICECHANGE and trying to get info on the USB device (LittleWire ). I think I got the basic figured out, but do not quite get the casting from from wParam to device "dbcc_name".
From PDEV_BROADCAST_HDR to PDEV_BROADCAST_DEVICEINTERFACE.
How does C++ compiler does this since the structures definitions are not same?
</pre>
<small></small> if ( DBT_DEVICEARRIVAL == wParam || DBT_DEVICEREMOVECOMPLETE == wParam ) {
// cast to generic structure
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
// define specific device type structures
PDEV_BROADCAST_DEVICEINTERFACE pDevInf;
PDEV_BROADCAST_HANDLE pDevHnd;
PDEV_BROADCAST_OEM pDevOem;
PDEV_BROADCAST_PORT pDevPort;
PDEV_BROADCAST_VOLUME pDevVolume;
// analyze specific device type
switch( pHdr->dbch_devicetype ) {
case DBT_DEVTYP_DEVICEINTERFACE:
{
// cast to DBT_DEVTYP_DEVICEINTERFACE
pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
CString strName = pDevInf->dbcc_name;
//CString strGuid = pDevInf->dbcc_classguid;
TRACE("\nDEvice full name %s", strName);
//TRACE("\nDEvicre full guid %s", strGuid);
|
|
|
|
|
Casting in C++ does not change anything, it just tells the compiler that the basic type or pointer that you have defined will actually be used to point to something else. It is your responsibility to know what you are doing. Windows messages pass pointers in the WPARAM or LPARAM fields, and you need to cast them to the correct type before you can access the structure that they point to. In your case above, you cast the lParam item to the generic PDEV_BROADCAST_HDR so you can find out what type it is, and then you cast that pointer to the specific structure pointer as defined in the header.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Thanks,
you helped me to realize that the "orignal" lParam PDEV_BROADCAST_HDR structure contains the PDEV_BROADCAST_DEVICEINTERFACE structure.
So the cast did not point to anything unknown.
I missed that.
Thanks
|
|
|
|
|
Yeah, you often get neted structs in Windows, with a base type at the start and other structs after that in memory, so there is lots of casting rom one type to another whcih is just to access the 'extended' data.
==============================
Nothing to say.
|
|
|
|
|
Dear Friends,
I have 11thousand PDF file and I need to take a log of number page in each PDF document and I decided to make tool to read the number pages in each PDF document. Anyone can help me. I have VC6 and visual studio 2008.
Thanks and Regards,
S.Shanmga Raja
|
|
|
|
|
You need to get hold of one of the PDF libraries available on the internet, or use the documentation available on the Adobe website to write your own. You can then read your PDF files and get information about the content.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi Shanmuga,
As Richard suggested, you've to use some thirdparty PDF SDK/library to get the pdf page count.
One option would be - QuickPDF Library[^] which is a absolutly free.
You can use the api - DPLPageCount()[^] to get the page count.
Have a look at their samples(well documented) about how to initialize the library and use it.
Hope this helps.
Best Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks.
|
|
|
|
|
As you may already know, new string literals in C++ 11 can be expressed in a very flexible way.
R"<delim>...<delim>"; - in this code the <delim> can be pretty much everything and also no escape characters are needed. Any kind of parentheses can be used to delimit the end of string, Raw string literals are especially useful when defining regular expressions:
R"(I love those who yearn for the impossible. (Von Goethe, "Faust"))";
Blocks of text can be simply defined using equal occurrences of same characters:
R";***************************( ; TINY BASIC FOR INTEL 8080
; VERSION 2.0
; BY LI-CHEN WANG
; MODIFIED AND TRANSLATED
; TO INTEL MNEMONICS
; BY ROGER RAUSKOLB
; 10 OCTOBER, 1976
; @COPYLEFT
; ALL WRONGS RESERVED ) ;***************************";
More information can be found here http://en.wikipedia.org/wiki/C++0x#New_string_literals[^] (Wikipedia).
I want to implement it with C++ processor, here is what I have. Now double quotation marks are not supported, and I need to compete it like C++ 11. Can anyone help me?
#define STR(a) #a
#define R(var, re) static char var##_[] = STR(re);\
const char * var = ( var##_[ sizeof(var##_) - 2] = '\0', (var##_ + 1) );
You can use it like this:
R(re, "\w\d");
The code is not friendly to use. So I want the feature like C++ 11 raw string literals.
PS: I'm using Visual C++ 2010, the new feature is not supported, I must implement it by myself.
|
|
|
|
|
I understand that the reason the ret variable will not print (it's at the end of the string when destination is passed) but can't figure out how to return destination out of the my_strcpy() function. I can make this work using array notation but not pointer arithmetic. Here's the code in question:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *my_strcpy(char *, const char *);
int
main()
{
char *strA = "This is a string";
char *strB;
char *ret;
strB = malloc(strlen(strA)+1);
ret = my_strcpy(strB, strA);
puts(ret);
puts(strB);
free(strB);
return 0;
}
char *my_strcpy(char *destination, const char *source)
{
while (*source != '\0')
{
*(destination++) = *(source++);
}
*destination = '\0';
return destination;
}
|
|
|
|
|
Just use a local variable for copying or save the value of destination in a local variable to be returned upon exit:
char *my_strcpy(char *destination, const char *source)
{
char *ret = destination;
while (*source != '\0')
{
*destination++ = *source++;
}
*destination = '\0';
return ret;
}
|
|
|
|
|
In addition to Jochen's comments, you also need to allocate some memory to strB before calling your copy function. The code above will be copying into some random place and probably crash.
One of these days I'm going to think of a really clever signature.
|
|
|
|